如何验证按钮单击时的复选框。 [英] How to validate checkboxes on button click.
本文介绍了如何验证按钮单击时的复选框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好专家,
如何验证asp.net c#按钮点击的复选框。
我的页面上有3个复选框。 (注意:这不是复选框列表,这些是单独的复选框)
当我点击提交按钮时,必须显示消息请选择至少一个复选框。
感谢您的时间。
解决方案
< script type = text / javascript >
function valid()
{
var a = document.getElementById(' <%=&chek1.ClientID GT;'跨度>);
var b = document.getElementById(' <%=&chek2.ClientID GT;'跨度>);
var c = document.getElementById(' <%=&chek3.ClientID GT;'跨度>);
if (a。 checked == false && b。已检查 == false && c。 checked == false )
{
alert( 选择至少一个);
return false ;
}
其他
{
返回 真跨度>;
}
}
< / script >
从按钮onclient事件< asp:调用此脚本button id = b1 onclintclick = return valid() xmlns:asp = #未知 > < / asp :按钮 >
您可以在服务器端/客户端验证
服务器端
支票
if (chb1。 checked == true || chb2。已检查 == true || chb3。已检查 == true )
{
}
else
{
ClientScript.RegisterStartupScript( this .GetType(),Guid.NewGuid()。ToString(), alert('检查至少一个'), true );
}
客户端
----
onclientclick =javascript:return vald();
<脚本>
function vald(){
if (
(' #<%= chk1.ClientID%>' ).is(' :checked')||
Hello experts,
How to validate checkboxes on button click in asp.net c#.
I have 3 checkboxes on my page. (Note : This is not checkboxlist, These are individual checkboxes)
When i click on submit button, Message must be displayed Please select atleast one checkbox.
Thanks for your time.
解决方案
<script type="text/javascript"> function valid() { var a=document.getElementById('<%=chek1.ClientID>'); var b=document.getElementById('<%=chek2.ClientID>'); var c=document.getElementById('<%=chek3.ClientID>'); if(a.checked==false && b.checked==false && c.checked==false) { alert("Select atleast one"); return false; } else { return true; } } </script>
call this script from button onclient event<asp:button id="b1" onclintclick="return valid()" xmlns:asp="#unknown"></asp:button>
You can either validate in serverside / clientside
in Server side
check
if(chb1.checked==true ||chb2.checked==true ||chb3.checked==true) { } else { ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Check atleast one')", true); }
in clientside
----
onclientclick="javascript:return vald();"
<script> function vald(){ if(
('#<%= chk1.ClientID %>').is(':checked')||
这篇关于如何验证按钮单击时的复选框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文