如何使用 JQuery 使文本框在更改时启用和禁用

How to make textbox enable and disable on change using JQuery

本文关键字:启用 JQuery 何使用 文本      更新时间:2023-09-26

我有一段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>
<div id="ccit" class="leaf">
 <label class="control input text" title="">
    <span class="wrap">ccit</span>
     <input class="" type="text" value="[Null]"/>
    <span class="warning"></span>
 </label>
</div>

$(document).ready(function () {        
    alert("hello");        
    $("#cca").on('change', 'label.control input', function (event) {
        alert('I am pretty sure the text box changed');
        $("ccit").prop('disabled',true);
        event.preventDefault();
    });
});

对于一个你$("ccit")上缺少#

$(document).ready(function () {        
    alert("hello");        
    $("#cca").change(function(){
        alert('I am pretty sure the text box changed');
        $("#ccit").prop('disabled',true);
        event.preventDefault();
    });
});

http://jsfiddle.net/qS4RE/

更新

$(document).ready(function () {               
    $("#cca").on('change', 'label.control input', function (event) {
        alert('I am pretty sure the text box changed');
        $("#ccit").find('label.control input').prop('disabled',true);
        event.preventDefault();
    });
});

http://jsfiddle.net/qS4RE/1/

更新 2

$(document).ready(function () {               
    $(document).on('change', '#cca label.control input', function (event) {
        alert('I am pretty sure the text box changed');
        $("#ccit").find('label.control input').prop('disabled',true);
        event.preventDefault();
    });
});
$("#ccit").attr('disabled','disabled');

您也可以在行上使用attr $("ccit").attr('disabled',true);

如果prop给您一个错误