简体   繁体   English

我想使用javascript将不同的URL链接分配给激活单选按钮时产生的按钮

[英]I would like to use javascript to assign different url links to the button that is produced when radio button are activated

I am new to using javascript and I am struggling with this set of radio buttons. 我对使用javascript并不陌生,但是我在这套单选按钮上苦苦挣扎。 What I would ultimately like to achieve is to have the images control the radio button (as they do) and populate different buttons with different links that lead to different product pages. 我最终想要实现的是让图像控制单选按钮(就像它们一样),并用指向不同产品页面的不同链接填充不同的按钮。

This is what I've come up with so far, but I can't figure out how to assign different urls because what I have is generating only one button with different labels. 到目前为止,这是我想出的,但是我无法弄清楚如何分配不同的url,因为我只生成了一个带有不同标签的按钮。

Please help! 请帮忙!

 function test() { var color = document.getElementsByName("place"); var found = 1; for (var i = 0; i < color.length; i++) { if (color[i].checked) { document.getElementById("choice1").value = color[i].value; found = 0; break; } else { found = 1; } } if (found == 1) { alert("Please Select Radio"); } } 
 .vertical{background-image:url(https://static1.squarespace.com/static/5a53e67ce5dd5b1bd751d9bb/t/5aa0294cc83025dd2277db31/1520445774361/ccpop_orientation5.jpg);} .horizontal{background-image:url(https://static1.squarespace.com/static/5a53e67ce5dd5b1bd751d9bb/t/5aa02946652dea43a4a1b63e/1520445768211/ccpop_orientation4.jpg);} .selector input{ margin:0;padding:0; -webkit-appearance:none; -moz-appearance:none; appearance:none; } .selector-2 input{ position:absolute; z-index:999; } .selector-2 input:active +.orientation, .selector input:active +.orientation{opacity: .9;} .selector-2 input:checked +.orientation, .selector input:checked +.orientation{ -webkit-filter: none; -moz-filter: none; filter: none; } .orientation{ cursor:pointer; background-size:contain; background-repeat:no-repeat; display:inline-block; width:120px;height:80px; -webkit-transition: all 100ms ease-in; -moz-transition: all 100ms ease-in; transition: all 100ms ease-in; -webkit-filter: brightness(1.8) grayscale(1) opacity(.7); -moz-filter: brightness(1.8) grayscale(1) opacity(.7); filter: brightness(1.8) grayscale(1) opacity(.7); } .orientation:hover{ -webkit-filter: brightness(1.2) grayscale(.5) opacity(.9); -moz-filter: brightness(1.2) grayscale(.5) opacity(.9); filter: brightness(1.2) grayscale(.5) opacity(.9); } /* Extras */ a:visited{color:#888} a{color:#444;text-decoration:none;} p{margin-bottom:.3em;} 
 <div class="selector"> <input id="vertical" type="radio" name="place" value="vertical" onclick="test()" style="border: none;"> <label class="orientation vertical" for="vertical"></label> <input id="horizontal" type="radio" name="place" value="horizontal" onclick="test()" style="border: none;" > <label class="orientation horizontal" for="horizontal"></label> </div> <br> <!-- BOTÃO DE CHECK --> <br> <input type="button" id="choice1"> 

When one of your radio buttons is clicked, this code will assign a value to the "choice1" button's data-url attribute. 单击您的单选按钮之一时,此代码将为“ choice1”按钮的data-url属性分配一个值。

When choice1 is clicked later, the url saved in its data-url attribute will be loaded into the current browser window. 稍后单击choice1时,保存在其data-url属性中的url将被加载到当前浏览器窗口中。

 function test() { var color = document.getElementsByName("place"); var found = 1; var btn = document.getElementById("choice1"); for (var i = 0; i < color.length; i++) { if (color[i].checked) { btn.value = color[i].value; btn.dataset.url = "https://google.com/search?q=" + color[i].value; //btn.dataset.url = "/somepage?color=" + i + "&colorValue=" + color[i].value; found = 0; break; } else { found = 1; } } if (found == 1) { alert("Please Select Radio"); } } document.querySelector("#choice1").addEventListener('click', function () { if (this.dataset.url) window.location.href = this.dataset.url; }); 
 .vertical{background-image:url(https://static1.squarespace.com/static/5a53e67ce5dd5b1bd751d9bb/t/5aa0294cc83025dd2277db31/1520445774361/ccpop_orientation5.jpg);} .horizontal{background-image:url(https://static1.squarespace.com/static/5a53e67ce5dd5b1bd751d9bb/t/5aa02946652dea43a4a1b63e/1520445768211/ccpop_orientation4.jpg);} .selector input{ margin:0;padding:0; -webkit-appearance:none; -moz-appearance:none; appearance:none; } .selector-2 input{ position:absolute; z-index:999; } .selector-2 input:active +.orientation, .selector input:active +.orientation{opacity: .9;} .selector-2 input:checked +.orientation, .selector input:checked +.orientation{ -webkit-filter: none; -moz-filter: none; filter: none; } .orientation{ cursor:pointer; background-size:contain; background-repeat:no-repeat; display:inline-block; width:120px;height:80px; -webkit-transition: all 100ms ease-in; -moz-transition: all 100ms ease-in; transition: all 100ms ease-in; -webkit-filter: brightness(1.8) grayscale(1) opacity(.7); -moz-filter: brightness(1.8) grayscale(1) opacity(.7); filter: brightness(1.8) grayscale(1) opacity(.7); } .orientation:hover{ -webkit-filter: brightness(1.2) grayscale(.5) opacity(.9); -moz-filter: brightness(1.2) grayscale(.5) opacity(.9); filter: brightness(1.2) grayscale(.5) opacity(.9); } /* Extras */ a:visited{color:#888} a{color:#444;text-decoration:none;} p{margin-bottom:.3em;} 
 <div class="selector"> <input id="vertical" type="radio" name="place" value="vertical" onclick="test()" style="border: none;"> <label class="orientation vertical" for="vertical" onchange="window.location.replace('www.google.com')"></label> <input id="horizontal" type="radio" name="place" value="horizontal" onclick="test()" style="border: none;" > <label class="orientation horizontal" for="horizontal"></label> </div> <br> <!-- BOTÃO DE CHECK --> <br> <input type="button" id="choice1"> 

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

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