基于复选框在iframe上传递URL

Pass URL on an iframe based on checkbox

本文关键字:URL iframe 复选框      更新时间:2023-09-26

对不起,我很不擅长JavaScript,我刚刚开始学习它。我有这样一个场景:我想传递一个URL到我的iframe,那么我该如何创建一个脚本呢?

<script>
  /* Pseudocode 
  
   if var variable1 is true then pass url to iframe with variable1 as value
   
   else if var variable2 is true then pass url to iframe with variable2 as value
   
   else let the default url
  
  */
</script>
Hope someone can know this.
Thank you!
<input type="checkbox" name="vehicle" value="(variable1)"> 
<input type="checkbox" name="vehicle" value="(variable2)"> 
<iframe id="javascriptID" src="(variable(1or2)">
</iframe>

你可以使用单选按钮,然后这样做:

HTTPSNow
<input type="radio" name="vehicle" value="https://www.httpsnow.org/" onclick="setURL(this.value)"/> 
<br/>
Example.com
<input type="radio" name="vehicle" value="https://www.example.com/" onclick="setURL(this.value)"/> 
<br/>
<iframe id="javascriptID" src="" width="500px" height="500px">
</iframe>
function setURL(thelink){
    javascriptID.src=thelink;
}

这里是JSFiddle演示

如果你仍然想使用复选框,你可以使用这个- jQuery:

$("input[name=vehicle]").change(function(){
    if($(this).is(":checked")){
        // do some thing here
    }
})

但我认为这不是一个好主意,因为复选框不是唯一的