簡體   English   中英

如何使用 select 選項更改 iframe src?

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

我想你可以看到我的想法;)我怎樣才能做到這一點? 謝謝;)

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

您可以使用sel.value屬性

這是我將使用的代碼:

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

很容易,只需定位您的選擇框並使用querySelector獲取它的值

嘗試片段

 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