How to make text box enable and disable in jquery

This question already has an answer here:

  • Disable/enable an input with jQuery? 11 answers

  • 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:

    This will fire when text box is left

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

    This, will fire on 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();
        });
    });
    

    This, will fire on keyup as typing

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

    See Demo on JsFiddle

    You should also close your input:

    <input class="" type="text" value="[Null]"/>
    

    You're listening for the change event, which will not fire until the input loses focus. Given that the code you've provided does trigger an alert once the focus leaves the input (by tab or click), I'm guessing you were expecting a response after typing but before changing focus. To accomplish that, listen for the input event instead.

    链接地址: http://www.djcxy.com/p/22998.html

    上一篇: 使用jquery禁用名称属性的textarea字段

    下一篇: 如何使文本框启用和禁用jQuery中