本文介绍了验证复选框内$使用Javascript的GridView p $ psent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
This is my aspx code...
<asp:TemplateField HeaderText="IsExist">
<ItemTemplate>
<asp:CheckBox ID="chkExists" runat="server" Text="Exists" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Not Exists In Update">
<ItemTemplate>
<asp:CheckBox ID="chkExistsInUpdate" runat="server" Text="NotExists" AutoPostBack="false"/>
</ItemTemplate>
</asp:TemplateField>
Here I have 2 checkboxes but in different columns i.e. chkExist for Exist column and chkExistInUpdate for NotExist column in gridview. I want If they both checked then alert() mesg should display like "You can check Only one checkbox"..So here is my javascript code....
function check_one() {
var obj = document.form1;
var isValid = false;
var gridView = document.getElementById('<%= Gridview1.ClientID %>');
for (var i = 1; i < gridView.rows.length; i++) {
var inputs = gridView.rows[i].getElementsByTagName('input');
if (inputs == null) {
if (inputs[0].type == "chkExists" && inputs[0].type == "chkExistsInUpdate") {
if (inputs[0].checked && inputs[0].checked) {
isValid = true;
}
}
var ch1 = inputs[0].type["chkExists"];
}
//alert(inputs[0].type == "chkExists");
alert(ch1);
}
alert("Plese select only 1 check");
return false;
}
So plese suggest me that what changes has to do in javascript...
解决方案
You should use radio buttons, and set a group name dependent by row, because you have a group per row:
<asp:TemplateField HeaderText="IsExist">
<ItemTemplate>
<asp:RadioButton ID="chkExists" runat="server" Text="Exists" AutoPostBack="false" GroupName='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Not Exists In Update">
<ItemTemplate>
<asp:RadioButton ID="chkExistsInUpdate" runat="server" Text="NotExists" AutoPostBack="false" GroupName='<%# Eval("Id") %>'/>
</ItemTemplate>
</asp:TemplateField>
But, if you need checkboxes anyway (it doesn't make sense for me), you can use jQuery:
function check_one() {
$('#<%= Gridview1.ClientID %> tr').each(function() {
if(jQuery(":checkbox", this)
.filter(function(i) { return this.checked; }).length != 1) {
alert("Plese select only 1 check");
}
});
}
这篇关于验证复选框内$使用Javascript的GridView p $ psent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!