javascript - 如何在jquery中启用和禁用文本框

标签 javascript jquery html

<分区>

我写了一个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]"/>

关于javascript - 如何在jquery中启用和禁用文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658244/

相关文章:

javascript - 是否可以为 c3.js 加载新的 y 轴标签?

html - django 中的几页中的 base.html 中的图像未显示

javascript - 在 .each 函数中等待 $.ajax 结果

javascript - 使用 Promises 从 DynamoDB 异步检索数据

javascript - 为由 Web 服务填充的 ASP.NET TreeView 加载动画

Jquery UI 自动完成 : search from multiple attributes of one array

javascript - 单击另一个链接后单击链接

jquery - Bootstrap 3 : Affix navbar fade in and add object

html - 非 IE6 IE7 破解

javascript - 将焦点设置在下一个下拉列表而不是使用 blur() 方法?