传递一个JavaScript变量到HTML<选择>属性 [英] Passing a JavaScript variable to a html <select> attribute

查看:88
本文介绍了传递一个JavaScript变量到HTML<选择>属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从JavaScript传递一个变量看重选择标签的属性。这里是我的code:

 <脚本> 如果(window.location.href.substring(0,9)===的http:// WW){
     VAR home_var =bing.com;
     VAR home_var =poem.com;
 }否则如果(window.location.href.substring(0,9)===htt​​ps://开头P){
     VAR home_var =htt​​ps://px.multiscreensite.com/index.php?url=bing.com;
     VAR poem_var =htt​​ps://px.multiscreensite.com/index.php?url=poem.com;
 }其他{
     变种home_var =1;
     var_poem_var =2;
 }
 的console.log(home_var);< / SCRIPT><选择的OnChange =window.location.href = THIS.VALUE>
   <选项>标签的东西,导航和LT; /选项>
   <期权价值= HOME_VAR>主页< /选项>
< /选择>


解决方案

如果您添加SELECT后在JavaScript就可以实现这一点:

 <选择一个id =mySelect的OnChange =window.location.href = THIS.VALUE>
    <选项>标签的东西,导航和LT; /选项>
< /选择><脚本>
    如果(window.location.href.substring(0,9)===HTTP:// WW){
    VAR home_var =bing.com;
    }
    否则,如果(window.location.href.substring(0,9)===htt​​ps://开头P){
    VAR home_var =htt​​ps://px.multiscreensite.com/index.php?url=bing.com;
    }
    其他{
    变种home_var =1;
    }
    VAR选择=的document.getElementById(mySelect);
    VAR选项=使用document.createElement(选项);
    option.text =希望页;
    option.value = home_var;
    select.add(选件);
    的console.log(home_var);
< / SCRIPT>

JS小提琴 https://jsfiddle.net/emfvyhq2/

I am trying to pass a variable from JavaScript to value attribute of the select tag. Here is my code:

<script>

 if (window.location.href.substring(0, 9) === "http://ww") {
     var home_var = "bing.com";
     var home_var = "poem.com";
 } else if (window.location.href.substring(0, 9) === "https://p") {
     var home_var = "https://px.multiscreensite.com/index.php?url=bing.com";
     var poem_var = "https://px.multiscreensite.com/index.php?url=poem.com";
 } else {
     var home_var = "1";
     var_poem_var = "2";
 }
 console.log(home_var);

</script>

<select OnChange="window.location.href=this.value">
   <option>Tagged Stuff and Navigation</option>
   <option value=HOME_VAR>Home Page</option>
</select>

解决方案

If you add the SELECT after in javascript you can achieve this:

<select id="mySelect" OnChange="window.location.href=this.value">
    <option>Tagged Stuff and Navigation</option>
</select>

<script>
    if (window.location.href.substring(0,9) === "http://ww") {
    var home_var = "bing.com";    
    }
    else if (window.location.href.substring(0,9) === "https://p") {
    var home_var = "https://px.multiscreensite.com/index.php?url=bing.com";
    }
    else {
    var home_var = "1";
    }
    var select= document.getElementById("mySelect");
    var option = document.createElement("option");
    option.text = "Hope Page";
    option.value = home_var;
    select.add(option);
    console.log(home_var);
</script>

JS Fiddle https://jsfiddle.net/emfvyhq2/

这篇关于传递一个JavaScript变量到HTML&LT;选择&GT;属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆