What's the problem with this code?

三世轮回 提交于 2020-01-06 14:05:44

问题


Where to modify this code

IT STILL GIVES A MSGBOX IF I SELECT THE CHECKBOX BOX OR NOT ....

My code below will redirect to Google in both conditions: If the user selects the checkbox, then it will redirect to www.google.com, but if a user forgets to check the checkbox then it shows the msg box with an ok button. When I click on ok it should redirect to www.google.com

I want

When a user forgets to check any of the checkboxes to show a msgbox with an ok button and stay on the same page. Otherwise if user selects any of the checkboxes then redirect to www.google.com

What's wrong with this code?

    <title>Untitled Page</title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CheckBox ID="CheckBox2" runat="server" />

    </div>
    <asp:Button ID="Button1" runat="server" OnClientClick ="ConfirmSelection(this.form)" Text="Button" />


    </form>
    <script type="text/javascript">
function ConfirmSelection(frm) 
{ 
   for (i=0; i<=1; i++) {
     //chkSubjectOfInterest is the id of your checkbox control

     if (frm.elements[i].name.indexOf('chkSubjectOfInterest') !=-1) 
     { 
       if (frm.elements[i].checked) 
       { 
         return true
       } 
     } 
   } 
   alert('You havent selected an Item yet!')
   return false
}
</script>
</body>
</html>

回答1:


I think you need a return in your function call. Also ensure your function ConfirmSelection has explicit return value in both parts of the if statements

OnClientClick ="return ConfirmSelection(this.form);"


来源:https://stackoverflow.com/questions/4817704/whats-the-problem-with-this-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!