开发者

How to disable the button having similar Id using jquery

开发者 https://www.devze.com 2023-03-19 03:11 出处:网络
I have the 3 buttons id\'s like #button开发者_C百科1,#button2,#button3.Now I click #button2,here i want to disable the other 2 buttons that is #button1,#button3.We can use $(\"#myButton\").attr(\"disa

I have the 3 buttons id's like #button开发者_C百科1,#button2,#button3.Now I click #button2,here i want to disable the other 2 buttons that is #button1,#button3.We can use $("#myButton").attr("disabled", true); to disable the button.But here we should have some logic on this. Kindly help me.


You can get all three elements and remove the currently clicked one:

$('#button1,#button2,#button3').click(function(){
  $('#button1,#button2,#button3').not(this).attr('disabled',true);
});

Edit:

You can also use a variable to reduce the repetition, both in the code and the exectution:

var buttons = $('#button1,#button2,#button3');
buttons.click(function(){
  buttons.not(this).attr('disabled',true);
});

You might also consider using a class on the buttons instead of specifing each id, that generally makes it easier to maintain:

var buttons = $('.Radio-ish-button');
buttons.click(function(){
  buttons.not(this).attr('disabled',true);
});


try:

$("input[id^=button]").click(function(){
    $("input[id^=button]").not(this).attr('disabled',true);
});


$(function () {

  $('#button2').live('click', function () {
    $('#button1,#button3').attr('disabled', true);
  });

});

EDIT: after comments, try to use classes instead of id's

$(function () {

  $('.buttons').live('click', function () {
    $('.buttons').not(this).attr('disabled', true);
  });

});


You can do it by filtering the pressed button out from the tree button set.

we can use not on the list to filter out the button pressed.

$(document).ready(function() {
    $('#button1,#button2,#button3').click(function() {
        $('#button1,#button2,#button3').not(this).attr('disabled',true)
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消