<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 5 年前。
标签 javascript jquery html css
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 5 年前。
我正处于一个困惑的境地。我有一个这样的 div
<div class="editable" id="{{ $company->id }}" data-attribute="description">
{{ $company->description }}
</div>
我实际上正在使用 jinplace
进行编辑,它需要一个 div 来像上面那样设置。我正在寻找一种方法来使用字符计数,它只适用于 input
类型元素
我试过这样设置
<div class="editable" id="{{ $company->id }}" data-attribute="description">
<input type="text" data-length="500" value="{{ $company->description }}"
</div>
它显示此 div 的字符数,但提交时不更新。
有什么办法可以设置吗?
最佳答案
jinplace
确实使用标准格式的数据,它使用 AJAX 参见 here它也不使用数据长度属性,请参阅 here .
相反,我会使用一些 jQuery,因为您已经在您的元素中使用了它,因为 jinplace
是一个 jQuery 插件。
使用标准contentEditable您可以简单地将任何元素变成可编辑的属性。
然后我会使用计数器来更新隐藏字段,以便您可以以标准方式将数据发布到服务器。
$(function () {
countDiv("#companyId");
});
function countDiv(sender) {
var html = $(sender).html();
var count = html.length;
var maxCount = $(sender).data("count");
if (count > maxCount) {
html = html.substring(0, maxCount);
$("#companyId").html(html);
count = maxCount;
}
$("#companyIdCount").text(count);
$("#hfCompanyId").text(html);
}
function test() {
console.log($("#hfCompanyId").html());
}
#companyId {
min-height: 100px;
border: solid 1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="hfCompanyId" name="hfCompanyId" />
<div contentEditable id="companyId" oninput="countDiv(this);" data-count="500">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas a mollis magna. Sed quis iaculis nisl. Integer eget leo pulvinar, scelerisque ante nec, euismod lorem.
</div>
<br />
Count: <span id="companyIdCount"></span>
<br /><br />
<input type="submit" onclick="test();" value="Test" />
关于javascript - 在输入字段中放置一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088364/
相关文章:
javascript - typescript如何解析@Angular/core/testing的导入以及Angular Core如何打包?
javascript - perl cgi jquery 不工作
javascript - 使用ajax在表单外部提交带有按钮的表单
javascript - 如何使用Javascript制作一个5秒后才会出现的按钮?
javascript - 如何使用java脚本创建的超链接添加附加参数,例如(rel ="Y"data-behavior ="X"data-token ="XXX"class ="XXX")
javascript - 使用css或jquery双击子元素时禁用双击父元素
javascript - 使用 $scope 变量的值来指定属性
javascript - 如何动态地将相同的值分配给表中的匹配行