Using javascript to link after checking terms and conditions are checked
enter code here`I am trying to make a link that goes to paypal, but the link will only work if a checkbox has been checked. This is my code:
HTML
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MDSGURBJXF5K6" onclick="return buy_link(this)">Buy hard copy</a>
Javascript
function buy_link(link){
agree_check = oForm.elements["agree"].value;
if(agree_check === true){
window.location = link.href;
}else{
alert("You must agree to the开发者_如何学C terms and conditions before purchasing.");
}
return false;
}
It seems to just link whatever happens? event if I comment out the window.location() function. Any idea's where I am going wrong?
You don't need to pass the link to buy_link()
. If the onClick
event handler returns false, the browser won't further process the event, and the href won't be used. If it returns true, it will.
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MDSGURBJXF5K6" onclick="return buy_link();">Buy hard copy</a>
function buy_link(){
agree_check = oForm.elements["agree"].checked;
if(!agree_check){
alert("You must agree to the terms and conditions before purchasing.");
}
return agree_check;
}
var agree_check = Form.elements["agree"].checked;
or just
if (Form.elements["agree"].checked)
{
Xavi López is correct about not passing the link to the function.
精彩评论