同意条款复选框

时间:2018-05-06 14:17:35

标签: javascript

如果选中该复选框,我想转到某个页面,但我无法使其正常工作。

我有这个:

<form action="#" onsubmit="if(document.getElementById('agree').checked) { 
window.location = "http://myrurl"; } else { alert('Blabla'); return false; }">
<input type="checkbox" name="checkbox" value="check" id="agree" /> I agree to the Terms
<input type="submit" name="submit" value="submit" />
</form>

2 个答案:

答案 0 :(得分:0)

您不应该使用if($alias != $product->alias){ $url = '/product/'.$id.'/'.$product->alias; return redirect($url); } 内联。跟随&amp;更难读。这会更容易和外面清洁。

一个例子:

js

答案 1 :(得分:0)

正如评论所指出的那样 - 不要使用内联js。 我也改变了你的HTML。

<form>
  <label for="agree">I agree to the Terms</label>
  <input type="checkbox" name="agree" id="agree" /> 
  <input type="submit" name="submit" value="submit" />
</form>
<script>
  var agree = document.getElementById('agree');
  agree.onchange = function() {
    if(agree.checked) {
      // do something
    } else {
      // do something else
    }
  };
</script>

你可以在这里找到更多: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox

祝你好运!