单击“继续”按钮之前复选框验证

时间:2014-12-27 19:57:59

标签: javascript validation checkbox

<a href="page.html" > <button type="button" id="proceed-button">Proceed </button></a>

<input type="checkbox" name="checkbox3" value="yes">
By checking the box, I certify that have read the above disclaimers and agree to the rules. </input>

我有一个复选框和一个按钮,它将带我到下一页。但是,在我按下按钮之前,必须勾选复选框。如果没有,则必须在复选框下方显示标签,说明&#34;首先接受规则&#34;。救命?此外,如果我点击继续而不选中复选框,我可以突出显示红色复选框。可以使用javascript / jquery。

2 个答案:

答案 0 :(得分:3)

试试这个有效

<form action="page.html">
<input type="checkbox" name="checkbox3" value="yes" required>
By checking the box, I certify that have read the above disclaimers and agree to the rules. </input>
<input type="submit" name ="submit"/>
</form>

答案 1 :(得分:-1)

为了帮助您入门:

<input id="checkboxAgree" type="checkbox" name="checkbox3" value="yes">
function checkAgree() 
{
   if (document.getElementbyId("checkboxAgree").getAttribute("checked") )//checkbox is checked
   {
       location.href = "page.html"; //load the next page.
   }
   else
   {
       Alert("You need to check the box before you can continue");
   }

}

document.getElementById("proceed-button").addEventListener("click", checkAgree ,false);

addEventListeneronclick添加button个事件。单击时,执行函数checkAgree。当复选框具有属性checked时,将检查该对象,if将呈现为真。 location.href将加载page.html

请删除按钮周围的a