【问题标题】:Javascript "if and else" statements [closed]Javascript“if and else”语句[关闭]
【发布时间】:2013-03-10 04:35:31
【问题描述】:

我有一个Terms of Service 页面,我想强制用户在单击接受按钮之前检查Checkbox。否则它应该显示一条弹出消息,说

请在继续之前阅读服务条款

如果选中并单击接受按钮,他们将重定向到官方网站。

谁能帮帮我?

【问题讨论】:

  • 你做了什么?有什么适合我们的吗?
  • 我们需要看看你目前拥有什么
  • 你不需要 JavaScript(除非你关心旧浏览器)<input type="checkbox" name="accept_tos" required title="Please read and accept the Terms of Service" />

标签: javascript jquery html css


【解决方案1】:

试试这个:

<SCRIPT>

//Accept terms & conditions script (by InsightEye www.insighteye.com)
//Visit JavaScript Kit (http://javascriptkit.com) for this script & more.

function checkCheckBox(f) {
  if (f.agree.checked == false ) {
    alert('Please check the box to continue.');
    return false;
  } else {
    return true;
  }
}
</SCRIPT>

<form action="/yourscript.cgi-or-your-page.html" method="GET"
 onsubmit="return checkCheckBox(this)">

  <!--Enter your form contents here-->
  <b>By submitting, I agree that all info entered was done
     accurately & truthfully.</b><br>
     I accept: <input type="checkbox" value="0" name="agree">
  <input type="submit" value="Submit form">
  <input type="button" value="Exit" onclick="document.location.href='/index.html';">
</form>

或者这个:

<form name="frm">
  <input type="checkbox" name="chk" onClick="apply()">If you click this the specified
   url will open.
  <div align=center>&copy<a href="http://www.mine.com" style="color:#3D366F;text-decoration:none;cursor:pointer;font-size=13px">hscripts.com</a></div>
</form>
<script type="text/javascript">
  function apply() {
   if(document.frm.chk.checked==true) {
     window.open("http://www.mine.com/license/license.html");
   }
  }
</script>

【讨论】:

  • 这正是我想要的,谢谢!
  • 这些都不是特别“安全”的。在第一种情况下,如果禁用了 javascript,则表单将提交,因此必须在服务器上进行检查以确定复选框是否被选中。在第二种情况下,用户可以简单地复制 href 以访问该页面。在这两种情况下,一旦用户到达链接,如何阻止他们以后直接访问该 URL?
  • 如果你有 solution.plz 提供。
【解决方案2】:

在submit handler 中检查此condition。

if (formname.checkboxname.checked == true)
   {
      //showalert

      return false;
   }else{

     //open your page
  }

【讨论】:

  • 这更容易理解谢谢
  • 干杯 :)如果您觉得有用,请标记为答案。
猜你喜欢
  • 2017-09-05
  • 2016-10-15
  • 1970-01-01
  • 2021-09-11
  • 2018-03-29
  • 2017-09-25
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多