[英]On Key press cancel change event of text box and on button click trigger change event of text box jquery
我想取消按键时文本框的更改事件,然后单击按钮时触发它。 这是我的HTML的示例
$('#btn1').on('click', function() { var target = '#test'; $(target).on("change"); $(target).focus(); $(target).val('HELLO').trigger('change'); }); $("#test").change(function(event) { alert($(this).val()) }); $('#test').on('keypress', function(event) { if (event.keyCode === 13) { event.stopImmediatePropagation(); $(this).off("change"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="test" /> <input type="submit" id="btn1" value="BUTTON1" />
但这不是触发更改事件。
请提出建议。谢谢。
$('#btn1').on('click', function () {
var target='#test';
$(target).on("change",changeText($(target)));
$(target).focus();
$(target).val('HELLO').trigger('change');
});
function changeText($this) {
alert($this.val())
}
$('#test').on('keypress', function (event) {
event.stopImmediatePropagation();
$(this).off("change");
});
事件不在函数参数中,并尝试使用传播https://jsfiddle.net/0624eq86/20/
我认为您忘记了在JavaScript代码中添加“事件”
替换:
$('#test').on('keypress', function() {
event.stopImmediatePropagation();
$(this).off("change");
});
与:
$('#test').on('keypress', function(event) {
event.stopImmediatePropagation();
$(this).off("change");
});
function test(val){ alert(val); } $('#btn1').on('click', function() { var target = '#test'; $(target).focus(); $(target).val('HELLO').trigger('change'); $(target).on("change", test($(target).val())); }); $('#test').on('keypress', function(event) { event.stopImmediatePropagation(); $(this).off("change"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="test" /> <input type="submit" id="btn1" value="BUTTON1" />
请检查答案,希望这对您有用。 我已经创建了一个功能测试,当我在点击功能上ON
更改事件时会调用该功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.