根据复选框在iframe上传递URL

时间:2015-07-27 07:37:39

标签: javascript html iframe

抱歉,我对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>

2 个答案:

答案 0 :(得分:1)

您可以改用单选按钮,然后执行以下操作:

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

Here is the JSFiddle demo

答案 1 :(得分:0)

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

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

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

相关问题