2011-02-06 51 views
0
<input type="radio" id="checkMain" /> 
<input type="radio" id="checkMain1" /> 
<input type="radio" id="checkMain2" /> 

$(function() 
    { 
    $("input[id=checkMain]").click(function() { 
     var otherCks = $("input[id^=checkMain]").not(this); 
     if (!$(this).is(":checked")) { 
      $(".child").attr("disabled", true); 
      otherCks.removeAttr("disabled"); 
     } else { 
      $(".child").removeAttr("disabled"); 
      otherCks.attr("disabled", true) 
     } 
    }); 
    $("#chk_all").click(function() { 
     var checked_status = this.checked; 
     $("input[id^=chk]").each(function() { 
      this.checked = checked_status; 
     }); 
    }); 
}); 

$(function() 
    { 
    $("input[id=checkMain1]").click(function() { 
     var otherCks = $("input[id^=checkMain]").not(this); 
     if (!$(this).is(":checked")) { 
      $(".child,.child1").attr("disabled", true); 
      otherCks.removeAttr("disabled"); 
     } else { 
      $(".child,.child1").removeAttr("disabled"); 
       otherCks.attr("disabled", true) 
     } 
    }); 
    $("#chk_all").click(function() { 
     var checked_status = this.checked; 
     $("input[id^=chk]").each(function() { 
      this.checked = checked_status; 
     }); 
    }); 
}); 


$(function() 
    { 
     $("input[id=checkMain2]").click(function() { 
      var otherCks = $("input[id^=checkMain]").not(this); 
      if (!$(this).is(":checked")) { 
       $(".child,.child1").attr("disabled", true); 
       otherCks.removeAttr("disabled"); 
      } else { 
       $(".child,.child1").removeAttr("disabled"); 
       otherCks.attr("disabled", true) 
      } 
     }); 
     $("#chk_all").click(function() { 
      var checked_status = this.checked; 
      $("input[id^=chk]").each(function() { 
       this.checked = checked_status; 
      }); 
     }); 
    }); 

示例代碼上面的代碼完全適用於複選框,但單選按鈕單選按鈕不會被禁用一旦點擊了<a href="http://jsfiddle.net/harshilshah/sBgZw/23" rel="nofollow">http://jsfiddle.net/harshilshah/sBgZw/23</a></p> <p>

總之,工作應該是這樣的dosent工作:當我點擊radiobutton1時,其他人應該被禁用(不可用)。當我重新點擊radiobutton1時,其他人應該再次可用。

+0

http://jsfiddle.net/harshilshah/sBgZw/23/ – Harshil 2011-02-06 10:20:33

+0

中的示例代碼你能更具體嗎?單擊時,您想要禁用哪個確切的單選按鈕? – AbdullahC 2011-02-06 10:22:59

+0

上面的代碼完美適用於複選框,但單選按鈕 – Harshil 2011-02-06 10:23:17

回答

0

你的小提琴和上面的代碼不匹配,但小提琴本身完美工作(複選框)。

對於無線電,這種替換也適用。

<input type="radio" id="chkMain" name="chkMain"/> 
<input type="radio" id="chkMain1" name="chkMain" /> 
<input type="radio" id="chkMain2" name="chkMain" /> 
<input class="child" type="radio" name="chkChild" id="chk1" disabled="true" /> 
<input class="child" type="radio" name="chkChild" id="chk2" disabled="true" /> 
<input class="child" type="radio" name="chkChild" id="chk3" disabled="true" /> 
<input class="child" type="radio" name="chkChild" id="chk4" disabled="true" /> 
<input class="child" type="radio" name="chkChild" id="chk5" disabled="true" /> 
<input class="child" type="radio" name="chkChild" id="chk6" disabled="true" /> 
<input class="child" type="radio" name="chkChild" id="chk7" disabled="true" /> 

顯然但是,一旦你關閉其他的單選按鈕,也沒有辦法「取消選擇所有」,也不是「另一種選擇」 - 不正常。

如果您使用此代碼來標記用假類的單選按鈕,您可以切換按鈕

$("input[id^='chkMain']").click(function() { 
    if ($(this).hasClass('checked')) { 
     $(this).attr("checked",false); 
     $(this).removeClass('checked'); 
    } 
    else { 
     $(this).addClass('checked'); 
    } 
    $("input[id^='chkMain']").not(this).attr("disabled", $(this).is(":checked")); 
    $(".child").attr("disabled", !$(this).is(":checked")); 
}); 

EDIT(與HTML代碼中使用)

看看at

0
  1. 您不必重複代碼的每個元素(DRY)。只要做到:

    $(function() { 
        $("input[id^=checkMain]").click(function() { 
         var otherCks = $("input[id^=checkMain]").not(this); 
         if (!$(this).is(":checked")) { 
          $(this).attr("disabled", true); 
          otherCks.removeAttr("disabled"); 
         } else { 
          $(this).removeAttr("disabled"); 
          otherCks.attr("disabled", true) 
         } 
        }); 
    }); 
    
  2. 在你的代碼有$(".child").attr("disabled", true);但是元素沒有一個類child。只需將其替換爲this,無論如何它都更有意義(請參閱上面的代碼)。

  3. 即使如此,您不能再更改disabled狀態,因爲禁用的單選按鈕不會收到單擊事件。查看更新。

  4. 它的價值:DEMO。但它僅適用於第一次因3

更新中提到的原因:

我現在你的目標在什麼看。問題是再次點擊一個單選按鈕,不是取消選擇它。所以每次只會執行else。通過單擊另一個單選按鈕取消單選按鈕。但是,如果你禁用其他人,則無法取消選擇它。你可以跟蹤選定的狀態,否則手動取消選擇它,但這是值得的努力?

你爲什麼這樣做呢?似乎是用戶不必要的障礙。

相關問題