2013-02-18 181 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

相关问题