2013-02-18 137 views
1

我必須列出檢查時選中一個複選框,其他複選框同一行取消選中

<input type="checkbox" value="18" name="yes" id="yes1"> 
<input type="checkbox" value="13" name="yes" id="yes2"> 
<input type="checkbox" value="12" name="yes" id="yes3"> 

等列表複選框

<input type="checkbox" value="14" name="no" id="no2"> 
<input type="checkbox" value="18" name="no" id="no2"> 
<input type="checkbox" value="12" name="yo" id="no2"> 

,但我不知道在工作的時候檢查複選框ID yes1,id no1未檢查

+0

您的意思是給你的 「不」'input's一樣'id'?這是無效的HTML。 – ajp15243 2013-02-18 18:38:16

+0

您可能想看看ASP.Net Ajax ToolKit的[MutuallyExclusiveCheckBox](http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MutuallyExclusiveCheckBox/MutuallyExclusiveCheckBox.aspx)。在演示中,取消選中它旁邊的複選框。 – Win 2013-02-18 18:41:34

回答

1

假設您將您的複選框更改爲name="no",因爲您有重複的ID,並且name="yo"的錯誤如下所示:

<input type="checkbox" value="18" name="yes" id="yes1"> 
<input type="checkbox" value="13" name="yes" id="yes2"> 
<input type="checkbox" value="12" name="yes" id="yes3"> 

<input type="checkbox" value="14" name="no" id="no1"> 
<input type="checkbox" value="18" name="no" id="no2"> 
<input type="checkbox" value="12" name="yo" id="no3"> 

您可以用jQuery做這樣的:

$("input[name=yes]").click(function() { 
    var current = this.id.replace("yes",""); 
    if($(this).is(':checked')) { 
     $("#no" + current).prop('checked', false); 
    } 
}); 

$("input[name=no]").click(function() { 
    var current = this.id.replace("no",""); 
    if($(this).is(':checked')) { 
     $("#yes" + current).prop('checked', false); 
    } 
}); 
+0

感謝您的幫助。我只是做 – user2083943 2015-04-26 08:52:09

相關問題