我写了一个html和脚本的示例代码如下:
当我首先执行此代码时,我会收到该警报 hello,但当我通过按选项卡按钮在 cca 更改时会收到其他警报,然后它不会显示警报。如何使用该文本框以及启用和禁用它的其他文本字段。
HTML:
<div id="cca" class="leaf">
<label class="control input text" title="">
<span class="wrap">cca</span>
<input class="" type="text" value="[Null]">
<span class="warning"></span>
</label>
</div>
JS:
jQuery(document).ready(function () {
alert("hello");
jQuery("#cca label.control input").on('change', function (event) {
alert('I am pretty sure the text box changed');
event.preventDefault();
});
});
我不太确定您要做什么,但我可以帮助您启动警报。您基本上没有正确使用 jQuery“on”函数。
$('#thisNeedsToBeContainer').on('focusout', '#elemToBindEventTo', 函数(事件)....
以下其中一项将满足您的需要:
这将在文本框离开时触发
$(document).ready(function () {
alert("hello");
$("#cca").on('focusout', 'label.control input', function (event) {
alert('I am pretty sure the text box changed');
event.preventDefault();
});
});
这将在 change
上触发
$(document).ready(function () {
alert("hello");
$("#cca").on('change', 'label.control input', function (event) {
alert('I am pretty sure the text box changed');
event.preventDefault();
});
});
这将在键入时触发 keyup
$(document).ready(function () {
alert("hello");
$("#cca").on('onkeyup', 'label.control input', function (event) {
alert('I am pretty sure the text box changed');
event.preventDefault();
});
});
参见 JsFiddle 上的演示
您还应该关闭您的输入:
<input class="" type="text" value="[Null]"/>