我有一个被html属性readonly
设置为只读的输入字段,我正在使用jQuery .html()
方法将文本插入到该字段中:
<input type="text" id="myField" readonly="readonly" />
现在,我有了一个函数myFunction()
,我希望在jQuery将文本插入该字段时调用该函数。
发布于 2017-07-19 15:56:47
为了达到您的要求,您可以使用val()
设置该值,然后触发一个change
事件,您可以将事件处理程序挂接到该事件:
$('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>
发布于 2017-07-19 15:59:35
我希望这能帮助你..。
<input type="text" id="myField" readonly="readonly" value=""/>
<script>
$('#myField').on('change', function(){
if($('#myField').val()!=""){
myFunction();
}
});
</script>
https://stackoverflow.com/questions/45184142
复制相似问题