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

javascript - Disabling inputs with jQuery not working - Stack Overflow

programmeradmin0浏览0评论

I am trying to disable some input fields using jQuery 1.4.2. This is a part of the HTML code:

<input type="text" disabled="disabled" name="nume" value="Text" />

This is the Javascript:

$('.salveaza').live('click', function(e){
    $.get(ajaxURL, $('form', itemCurent).serialize()+'&id='+itemCurent.data('id')+'&optiune=editeaza-categorie');

    // This is the code for disabeling inputs
    $('input', itemCurent).attr('disabled', 'disabled');

    itemCurent.removeClass('curent');
    $('.optiuniEditeaza', itemCurent).remove();

    e.preventDefault(); 
});

The above code doesn't work. Note that the inputs are added with jQuery, that's why I am using live, I tried all sorts of stuff.

I also tried firebug console to disable all input fields on the page and it's not working:

$('input').attr('disabled', 'disabled');

Any help will be apreciated, Thanks

I am trying to disable some input fields using jQuery 1.4.2. This is a part of the HTML code:

<input type="text" disabled="disabled" name="nume" value="Text" />

This is the Javascript:

$('.salveaza').live('click', function(e){
    $.get(ajaxURL, $('form', itemCurent).serialize()+'&id='+itemCurent.data('id')+'&optiune=editeaza-categorie');

    // This is the code for disabeling inputs
    $('input', itemCurent).attr('disabled', 'disabled');

    itemCurent.removeClass('curent');
    $('.optiuniEditeaza', itemCurent).remove();

    e.preventDefault(); 
});

The above code doesn't work. Note that the inputs are added with jQuery, that's why I am using live, I tried all sorts of stuff.

I also tried firebug console to disable all input fields on the page and it's not working:

$('input').attr('disabled', 'disabled');

Any help will be apreciated, Thanks

Share Improve this question edited Mar 4, 2012 at 2:13 rob74 5,26833 silver badges37 bronze badges asked Jun 1, 2010 at 17:34 AndreiAndrei 4,6174 gold badges28 silver badges25 bronze badges 2
  • That would seem to be the correct procedure for disabling an input element, as long as the input is a child of itemCurent. Are you certain that the code after your $.get() is running? Does the request succeed? – user113716 Commented Jun 1, 2010 at 18:02
  • Yes the input is a child of itemCurent, I also tried disabling all the inputs on the page and it just enables all of them. – Andrei Commented Jun 2, 2010 at 8:34
Add a ment  | 

4 Answers 4

Reset to default 3

This should do the trick:

Disable: $('#the-field-id').attr('disabled', true);

Enable: $('#the-field-id').removeAttr('disabled');

Use true instead of disabled:

$("input").attr("disabled", true);

Working example: http://jsfiddle/bEC8L/ (input is set to disable after 5 seconds)

jQuery sets properties instead of attributes if the given attribute is actually a property name, and "disabled" isn't a valid value for the disabled property.

Your piece of code seems ok, so the problem must be ing from another part of code. Put a break on the line where you disable and then run step by step.

Try this code. Onclick of button the textbox will be disabled.

 <script type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#fname").attr("disabled",false);
        $("#idDisable").click(function(){

            $("#fname").attr("disabled",true);

        });

    });
</script>

    <input type="button" id="idDisable" value="click to disable" name="Click"/>
    <input type="text" id="fname" />
发布评论

评论列表(0)

  1. 暂无评论