检查条款和条件后使用javascript链接

时间:2011-09-13 10:37:14

标签: javascript html checkbox

在这里输入代码。我正在尝试创建一个支持PayPal的链接,但只有选中了复选框后,该链接才会生效。这是我的代码:

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 terms and conditions before purchasing.");
  }
  return false;
}

似乎只是链接发生了什么?如果我注释掉window.location()函数的事件。我知道哪里出错了?

2 个答案:

答案 0 :(得分:1)

您无需将链接传递给buy_link()。如果onClick事件处理程序返回false,浏览器将不会进一步处理该事件,并且不会使用href。如果它返回true,它将会。

<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;
  }

答案 1 :(得分:0)

var agree_check = Form.elements["agree"].checked;

或只是

if (Form.elements["agree"].checked)
{

XaviLópez关于不将链接传递给函数是正确的。