Disable button in jQuery

后端 未结 11 1694
时光说笑
时光说笑 2020-11-28 02:04

My page creates multiple buttons as id = \'rbutton_\"+i+\"\'. Below is my code:

相关标签:
11条回答
  • 2020-11-28 02:36

    Try this code:
    HTML

    <button type='button' id = 'rbutton_'+i onclick="disable(i)">Click me</button>
    

    function

    function disable(i){
        $("#rbutton_"+i).attr("disabled","disabled");
    }
    

    Other solution with jquery

    $('button').click(function(){ 
        $(this).attr("disabled","disabled");
    });
    

    DEMO


    Other solution with pure javascript

    <button type='button' id = 'rbutton_1' onclick="disable(1)">Click me</button>
    
    <script>
    function disable(i){
     document.getElementById("rbutton_"+i).setAttribute("disabled","disabled");
    }
    </script>
    

    DEMO2

    0 讨论(0)
  • 2020-11-28 02:39

    Use .prop instead (and clean up your selector string):

    function disable(i){
        $("#rbutton_"+i).prop("disabled",true);
    }
    

    generated HTML:

    <button id="rbutton_1" onclick="disable(1)">Click me</button>
    <!-- wrap your onclick in quotes -->
    

    But the "best practices" approach is to use JavaScript event binding and this instead:

    $('.rbutton').on('click',function() {
        $(this).prop("disabled",true);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <button class="rbutton">Click me</button>

    http://jsfiddle.net/mblase75/2Nfu4/

    0 讨论(0)
  • 2020-11-28 02:40

    There are two things here, and the highest voted answer is technically correct as per the OPs question.

    Briefly summarized as:

    $("some sort of selector").prop("disabled", true | false);
    

    However should you be using jQuery UI (I know the OP wasn't but some people arriving here might be) then while this will disable the buttons click event it wont make the button appear disabled as per the UI styling.

    If you are using a jQuery UI styled button then it should be enabled / disabled via:

    $("some sort of selector").button("enable" | "disable");
    

    http://api.jqueryui.com/button/#method-disable

    0 讨论(0)
  • 2020-11-28 02:43

    Simply it's work fine, in HTML:

    <button type="button" id="btn_CommitAll"class="btn_CommitAll">save</button>
    

    In JQuery side put this function for disable button:

    function disableButton() {
        $('.btn_CommitAll').prop("disabled", true);
    }
    

    For enable button:

    function enableButton() {
        $('.btn_CommitAll').prop("disabled", false);
    }
    

    That's all.

    0 讨论(0)
  • 2020-11-28 02:46

    This is the simplest way in my opinion:

    // All buttons where id contains 'rbutton_'
    const $buttons = $("button[id*='rbutton_']");
    
    //Selected button onclick
    $buttons.click(function() {
        $(this).prop('disabled', true); //disable clicked button
    });
    
    //Enable button onclick
    $('#enable').click(() =>
        $buttons.prop('disabled', false) //enable all buttons
    );
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="rbutton_200">click</button>
    <button id="rbutton_201">click</button>
    <button id="rbutton_202">click</button>
    <button id="rbutton_203">click</button>
    <button id="rbutton_204">click</button>
    <button id="rbutton_205">click</button>
    <button id="enable">enable</button>

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