使用jQuery禁用多种形式之一的按钮

时间:2012-06-28 16:22:35

标签: jquery

我有很多这样的表格:

<form class="some-name">
    <input type="hidden" name="bill" value"flower" />
    <button value="0">Yes</button>
    <button value="1">No</button>
    <button value="2">No</button>
</form>

<form class="some-name">
    <input type="hidden" name="ben" value"men" />
    <button value="0">Yes</button>
    <button value="1">No</button>
    <button value="2">No</button>
</form>

我想要发生什么

当用户点击按钮时,我想要禁用该表单中的其他按钮。

目前我正在尝试使用这个jQuery,但它不起作用?

我背后的想法是当前元素是一个按钮,所以你想要它的父母是表格,然后是表格中的所有按钮?

$(document).on("click", ".some-name button", function(){
    $($(this)).parent().$("button").attr("disabled", true);
});

也尝试过:

$(document).on("click", ".some-name button", function(){
    $($(this)).parent("button").attr("disabled", true);
});

但两者都没有运气?

3 个答案:

答案 0 :(得分:4)

$(document).on("click", ".some-name button", function(){
    $(this) // currently clicked button
      .siblings("button")  // all neighbor of clicked button
      .prop("disabled", true);  // set disabled
});

相关参考:

答案 1 :(得分:0)

$($(this)).parent().$("button")

这不是有效的jQuery。你想要这样的东西:

$(this).parent().find('button')

答案 2 :(得分:0)

试试这个:

$(document).on("click", ".some-name button", function(){
     $(this).parent('form').children("button").attr("disabled", true);
});​

以下是jsfiddle