JQuery - Set Attribute value

前端 未结 6 555
旧时难觅i
旧时难觅i 2021-02-05 02:22

I have following HTML with two elements having the same name




        
相关标签:
6条回答
  • 2021-02-05 02:42

    $("#chk0") is refering to an element with the id chk0. You might try adding id's to the elements. Ids are unique even though the names are the same so that in jQuery you can access a single element by it's id.

    0 讨论(0)
  • 2021-02-05 02:45

    Seriously, just don't use jQuery for this. disabled is a boolean property of form elements that works perfectly in every major browser since 1997, and there is no possible way it could be simpler or more intuitive to change whether or not a form element is disabled.

    The simplest way of getting a reference to the checkbox would be to give it an id. Here's my suggested HTML:

    <input type="hidden" name="chk0" value="">
    <input type="checkbox" name="chk0" id="chk0_checkbox" value="true" disabled>
    

    And the line of JavaScript to make the check box enabled:

    document.getElementById("chk0_checkbox").disabled = false;
    

    If you prefer, you can instead use jQuery to get hold of the checkbox:

    $("#chk0_checkbox")[0].disabled = false;
    
    0 讨论(0)
  • 2021-02-05 02:49

    Use an ID to uniquely identify the checkbox. Your current example is trying to select the checkbox with an id of '#chk0':

    <input type="checkbox" id="chk0" name="chk0" value="true" disabled>

    $('#chk0').attr("disabled", "disabled");

    You'll also need to remove the attribute for disabled to enable the checkbox. Something like:

    $('#chk0').removeAttr("disabled");

    See the docs for removeAttr

    The value XHTML for disabling/enabling an input element is as follows:

    <input type="checkbox" id="chk0" name="chk0" value="true" disabled="disabled" />
    <input type="checkbox" id="chk0" name="chk0" value="true" />
    

    Note that it's the absence of the disabled attribute that makes the input element enabled.

    0 讨论(0)
  • 2021-02-05 02:50

    Some things before the actual code..

    the hash (#) you use as the selector is for IDs and not for names of elements. also the disabled attribute is not a true false scenario .. if it has disabled attribute it means that it is true .. you need to remove the attribute and not set it to false. Also there are the form selectors that identify specific types of items in a form ..

    so the code would be

    $("input:checkbox[name='chk0']").removeAttr('disabled');
    

    Bringing the answer up-to-date

    You should use the .prop() method (added since v1.6)

    $("input:checkbox[name='chk0']").prop('disabled', false); // to enable the checkbox
    

    and

    $("input:checkbox[name='chk0']").prop('disabled', true); // to disable the checkbox
    
    0 讨论(0)
  • 2021-02-05 02:51

    "True" and "False" do not work, to disable, set to value disabled.

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

    To enable, remove.

    $('.someElement').removeAttr('disabled');

    Also, don't worry about multiple items being selected, jQuery will operate on all of them that match. If you need just one you can use many things :first, :last, nth, etc.

    You are using name and not id as other mention -- remember, if you use id valid xhtml requires the ids be unique.

    0 讨论(0)
  • 2021-02-05 02:51

    You can add different classes to select, or select by type like this:

    $('input[type="checkbox"]').removeAttr("disabled");

    0 讨论(0)
提交回复
热议问题