我想将JavaScript中的变量传递给select标签的value属性。这里是我的代码:将JavaScript变量传递给html <select>属性
<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>
那么,有什么问题? – Timo
JavaScript变量不能直接在HTML中使用,你可能想要的是一个适当的事件处理程序。 – adeneo
@TimoSta当选择“主页”选项而不是去bing.com或https://px.multiscreensite.com/index.php?url=bing.com是去bing.com/home_var或https:/ /px.multiscreensite.com/home_var。 –