[英]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.