传递一个JavaScript变量到HTML&LT;选择&GT;属性 [英] Passing a JavaScript variable to a html <select> attribute
本文介绍了传递一个JavaScript变量到HTML&LT;选择&GT;属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从JavaScript传递一个变量看重选择标签的属性。这里是我的code:
&LT;脚本&GT; 如果(window.location.href.substring(0,9)===的http:// WW){
VAR home_var =bing.com;
VAR home_var =poem.com;
}否则如果(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;
}其他{
变种home_var =1;
var_poem_var =2;
}
的console.log(home_var);&LT; / SCRIPT&GT;&LT;选择的OnChange =window.location.href = THIS.VALUE&GT;
&LT;选项&GT;标签的东西,导航和LT; /选项&GT;
&LT;期权价值= HOME_VAR&GT;主页&LT; /选项&GT;
&LT; /选择&GT;
解决方案
如果您添加SELECT后在JavaScript就可以实现这一点:
&LT;选择一个id =mySelect的OnChange =window.location.href = THIS.VALUE&GT;
&LT;选项&GT;标签的东西,导航和LT; /选项&GT;
&LT; /选择&GT;&LT;脚本&GT;
如果(window.location.href.substring(0,9)===HTTP:// WW){
VAR home_var =bing.com;
}
否则,如果(window.location.href.substring(0,9)===https://开头P){
VAR home_var =https://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);
&LT; / SCRIPT&GT;
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屋!
查看全文