最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make textbox enable and disable on change using JQuery - Stack Overflow

programmeradmin0浏览0评论

I have piece of html code as well as script code. I need solution to handle on change event of one text box that disable act of inputting data in another text field. Could any one help me in regarding.

<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();
    });
});

I have piece of html code as well as script code. I need solution to handle on change event of one text box that disable act of inputting data in another text field. Could any one help me in regarding.

<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();
    });
});
Share Improve this question edited Dec 18, 2013 at 18:01 Jason P 27k3 gold badges32 silver badges45 bronze badges asked Dec 18, 2013 at 17:58 PallaviPallavi 3131 gold badge4 silver badges17 bronze badges 5
  • 2 What isn't working? Could it be that you don't have a <ccit></ccit> element ? – adeneo Commented Dec 18, 2013 at 18:00
  • Hi, this is my html code which i have written to make that script to perform action <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> – Pallavi Commented Dec 18, 2013 at 18:02
  • Now that you've posted the HTML, it should be $("#ccit"), hence my first comment – adeneo Commented Dec 18, 2013 at 18:02
  • the event.returnValue is deprecated. please use the standard event.preventDefault() instead is just a warning on an old version of jQuery but it is not the reason your code is not working.. Javascript is a client side language so I don't understand what you mean by its not working server side. Sorry I don't know how to help you at this point unless you can demonstrate exactly what is not working. – Trevor Commented Dec 19, 2013 at 13:46
  • @ThotaPallavi try my Update 2 that is the last thing I can think of. – Trevor Commented Dec 19, 2013 at 16:16
Add a comment  | 

3 Answers 3

Reset to default 10

For one you were missing # on your $("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/

Update

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

Update 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');

You can also use attr on the line $("ccit").attr('disabled',true);

if prop gives you an error

发布评论

评论列表(0)

  1. 暂无评论