有关Javascript的问题 [英] Problem regarding Javascript

查看:79
本文介绍了有关Javascript的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好

我有一个checkboxlist1和一个Submit按钮...

我需要复选框列表验证用户,请从复选框列表中至少选中一个复选框.如果用户未选中任何复选框,则单击
提交按钮,那么当他单击警报框的确定按钮时,我需要一条警报消息(请选择至少一个复选框),什么也没有发生,意味着该按钮不提交表单.

我的Java脚本代码如下...

good evening everbody

I have one checkboxlist1 and one Submit button ...

i need checkboxlist validation user select at least one checkbox checked from checkboxlist.when user not check any checkbox and he click
the submit button then i need one alert message (please select atleast one checkbox) when he click ok button of alert box nothing happen means button not submit the form.

my java script code is given below...

<script type="text/javascript">
     function test() {
         var isChecked = false;
         var checkBoxList1 = document.getElementById('<%= CheckBoxList2.ClientID %>');
         for (var r = 0; r < checkBoxList1.rows.length; r++) {
             var varCheckBox = checkBoxList1.rows[r].cells[0].childNodes[0]
             if (varCheckBox) {
                 if (varCheckBox.checked) {
                     isChecked = true;
                     break;
                 }
             }
         }
         if (isChecked) {
             return true;
         }
         else {
             confirm('Please select at least one Checkbox');
             return false;
         }
     }




我的按钮代码在下面给出




and my button code is given below

<asp:Button ID="Submit" runat="server" OnClientClick="test();" Text="Submit" onclick="Submit_Click" />





在我的代码中,当我选择复选框并单击提交"按钮时,它的工作效果很好, 但是当我没有选中任何复选框并单击提交按钮时,当我单击确定按钮然后提交按钮单击事件触发并且表单发送空白数据时,它将显示警报消息.当我单击警报框确定按钮然后单击按钮时,我希望此阶段不提交任何数据或不执行任何操作,并允许用户选中复选框.


请帮助我....我如何根据我的需要更正我的代码.





in my code when i select checkbox and click submit button its work good,
but when i did''nt check any checkbox and click submit button then it show the alert message when i click ok button then submit button click event fire and form send blank data.i want this stage when i click alert box ok button then button not submit any data or not do anything and allow the user select the checkboxes.


please help me....how i correct my code according to my need.

推荐答案

在OnClientClick中的test()之前添加返回值

Add return before the test() in the OnClientClick

<asp:Button ID="Submit" runat="server" OnClientClick="return test();" Text="Submit" onclick="Submit_Click" />



我还要说,使用JQuery可以简化您的代码,并使代码更具可读性和可维护性



I would say also that using JQuery would simplify your code and make more readable and maintainable


感谢您的回复....它的工作原理很好
Thanks Dear for your reply....its working nice


您可以使用onsubmit形式进行测试.当给定的方法将返回false时,提交将被取消.

You could perform the test in the form onsubmit. When the method given will return false, the submit is canceled.

<form onsubmit="return test();">
...
</form>




旁注:
如果找到复选框,则可以直接返回true.




Side note:
You could directly return true if you find a checkbox.

function test() {
         var isChecked = false;
         var checkBoxList1 = document.getElementById('<%= CheckBoxList2.ClientID %>');
         for (var r = 0; r < checkBoxList1.rows.length; r++) {
             var varCheckBox = checkBoxList1.rows[r].cells[0].childNodes[0]
             if (varCheckBox) {
                 if (varCheckBox.checked) {
                     return true;
                 }
             }
          // no selected checkbox found
          alert('Please select at least one Checkbox');
          return false;
         }
     }



祝你好运!



Good luck!


这篇关于有关Javascript的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆