简体   繁体   English

JavaScript:选中复选框时更改href值

[英]JavaScript: change href value when box is checked

I have a button that links to a product page, like so... 我有一个链接到产品页面的按钮,例如...

<a href="/product.html" id="link"><button>Buy This</button></a>

And I need to change the value of href when a box is checked. 当选中一个框时,我需要更改href的值。 This is what I have so far 这就是我到目前为止

document.getElementById(#option1).checked = true;
document.getElementById(#link).value = '/product/option1.html';

But it's not working. 但这不起作用。

Thanks! 谢谢! :) :)

Of course it's not working... 当然不行了...

  1. getElementById , not GetElementById - case matters! getElementById ,而不是GetElementById大小写很重要!

  2. 'option1' , not #option1 - I don't know where you got the idea that #option1 was right. 'option1'而不是#option1我不知道您从何处得知#option1是正确的。

  3. You are setting the checked property to true , meaning the checkbox will be set to checked. 您将checked属性设置true ,这意味着该复选框将被设置为checked。

  4. You have no event handler at all, so this code will only run once, when the page loads, and won't update when the user toggles the box. 您根本没有事件处理程序,因此该代码仅在页面加载时运行一次,并且在用户切换框时不会更新。

  5. A link's href is in its .href property, not its .value (it doesn't actually have a .value ) 链接的href位于其.href属性中,而不位于其.value (它实际上没有.value

  6. You have a <button> inside an <a> tag. 您在<a>标记内有一个<button> Not technically valid, although most browsers will work the way you'd expect. 尽管大多数浏览器都可以按您期望的方式运行,但从技术上讲并不有效。 This isn't a guarantee though, and you should either use a link, or a JS event handler on the button. 但是,这并不是保证,您应该在按钮上使用链接或JS事件处理程序。

With all that in mind, I present to you... 考虑到所有这些,我向您介绍...

<button id="link">Buy this</button>
<label><input type="checkbox" id="option1" /> Option text</label>
document.getElementById('link').onclick = function() {
    if( document.getElementById('option1').checked) {
        location.href = '/product/option1.html';
    }
    else {
        location.href = '/product.html';
    }
};

You added jquery as a tag, if you're using it, try this: 您将jquery添加为标签,如果正在使用,请尝试以下操作:

$(function(){
  $("body").on("change", "#option1", function(){
    $("#link").attr("href", "https://www.google.com");
  });
})

Here is a nice demo I made in JSFiddle... 这是我在JSFiddle中制作的一个不错的演示...

www.jsfiddle.net/Lvu5818f www.jsfiddle.net/Lvu5818f

document.getElementById("link").onclick = function() {
   var link = document.getElementById("link");
   var checkbox = document.getElementById("button");
   if(checkbox.checked){
      link.setAttribute("href", "#test2");
   }
   return true;
}

<a href="#test1"id="link"><button>Buy This</button></a>
    <br>
    <label><input type="checkbox" name="checkbox" value="true" id="button">Add an extra T-Shirt</label>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <div id="test1">This is some text a long way away...</div>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <div id="test2">This is some text even farther away...</div>
document.GetElementById('#link').href= '/product/option1.html';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM