首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想使用javascript将不同的url链接分配给单选按钮被激活时产生的按钮。

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

Stack Overflow用户
提问于 2018-03-14 02:41:31
回答 1查看 33关注 0票数 0

我刚开始使用javascript,我正在为这组单选按钮而苦苦挣扎。我最终想要实现的是让图像控制单选按钮(就像他们做的那样),并用不同的链接填充不同的按钮,这些链接会导致不同的产品页面。

这就是我到目前为止想出的方法,但我不知道如何分配不同的urls,因为我得到的只是生成一个具有不同标签的按钮。

请帮帮我!

代码语言:javascript
复制
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");
  } 
  
}
代码语言:javascript
复制
.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;}
代码语言:javascript
复制
<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">

EN

回答 1

Stack Overflow用户

发布于 2018-03-14 02:55:10

当您的某个单选按钮被单击时,此代码将为"choice1“按钮的data-url属性赋值。

当稍后单击choice1时,保存在其数据url属性中的url将加载到当前浏览器窗口中。

代码语言:javascript
复制
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;
});
代码语言:javascript
复制
.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;}
代码语言:javascript
复制
<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">

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49263614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档