简体   繁体   English

如何使用 select 选项更改 iframe src?

[英]how to change iframe src with select option?

I guess you can see the idea i have;) how can i make this work?我想你可以看到我的想法;)我怎样才能做到这一点? Thanks;)谢谢;)

<select name="test" onchange="myFunction();" >
  <option value="index.html">Home</option>
  <option vaule="contact.html">Contact</option>
</select>

<script>
function myFunction() {
  document.getElementById("iframe").src = 'test.value';
}
</script>


<iframe id="iframe"></iframe>

You can use sel.value property您可以使用sel.value属性

Here the code i would use:这是我将使用的代码:

<script>
function myFunction() {
  sel = document.getElementById("sel")
  if(sel.value === "index.html"){
      document.getElementById("iframe").src =    
'https://link';
  } else {
      document.getElementById("iframe").src = 
'https://anotherLink';
  }
}
</script>

easy just target your selectbox and get it's value using querySelector很容易,只需定位您的选择框并使用querySelector获取它的值

try the snippet尝试片段

 function myFunction() { var val = document.querySelector('select[name="test"]').options; document.getElementById("iframe").src = val[val.selectedIndex].value; console.log(document.getElementById("iframe").src); }
 <select name="test" onchange="myFunction();" > <option value="index.html">Home</option> <option value="contact.html">Contact</option> </select> <iframe id="iframe"></iframe>

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

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