开发者

Using javascript to link after checking terms and conditions are checked

开发者 https://www.devze.com 2023-04-04 07:12 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号