我有一个输入字段,它被 html 属性设置为只读 readonly
我正在使用 jQuery .html()
方法将文本插入该字段:
<input type="text" id="myField" readonly="readonly" />
现在我有一个函数myFunction()
,我希望在通过 jQuery 将文本插入到该字段时调用它。
最佳答案
要实现您的要求,您可以使用 val()
设置值,然后触发 change
事件,您可以将事件处理程序 Hook 到该事件:
$('button').click(function() {
$('#myField').val('foo').trigger('change');
});
$('#myField').change(function() {
console.log('value changed...');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="myField" readonly="readonly" />
<button>Set value</button>
关于html - 检测文本是否被jquery插入到输入字段然后运行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184142/