如何在jQuery中启用和禁用文本框

问题描述:

我已经编写了html和脚本的示例代码,如下所示: 当我首先执行此代码时,我会收到警报提示,但是当我通过按Tab键在cca处更改时会收到其他警报,然后它不会显示警报.如何使用该文本框以及启用和禁用它的其他文本字段.

I have written a sample code of html and script as follows: When i execute this code first i will get that alert hello but other alert when i change at cca by pressing tab button then it is not showing alert. How to use that text box and enable and disable other text fields of it.

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"功能.

I'm not really sure what you are trying to do but I can help get the alert working. You are basically not using jQuery "on" function correctly.

$('#thisNeedsToBeContainer').on('focusout', '#elemToBindEventTo', function (event)....

以下其中一项将满足您的需求:

One of the following will do what you need:

在离开文本框时将触发

$(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]"/>