【问题标题】:how to change iframe src with select option?如何使用选择选项更改 iframe src?
【发布时间】:2021-02-22 04:38:57
【问题描述】:

我想你可以看到我的想法;)我怎样才能做到这一点?谢谢 ;)

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

【问题讨论】:

    标签: html iframe


    【解决方案1】:

    只需定位您的选择框并使用querySelector 获取它的价值

    试试sn-p

    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>

    【讨论】:

      【解决方案2】:

      你可以使用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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多