JQuery On选择更改为变量赋值并重新加载脚本

时间:2012-05-11 10:44:53

标签: javascript jquery

是否有可能分配所选的select值并将其分配给脚本的变量并重新运行脚本?

这是我目前的代码示例:

<select onchange="reload_script;">
  <option value="123">Select 123</option>
  <option value="456">Select 456</option>
<select>

<div id="123">
    <a href="http://same.com/index.do?n=152991">link title</a>
    <br/><br/>
    <a href="http://some.com/">Link</a>
</div>
<script language="javascript" type="text/javascript">
var code = '123' // <-- THIS VARIABLE WOULD BE THE VALUE OF THE SELECTED DROPDOWN VALUE
</script>

请参阅:var code ='123'....当选择框被更改时,这就是改变了...然后我需要脚本重新运行...

这有可能吗?

2 个答案:

答案 0 :(得分:3)

如果你使用jQuery,你总是可以绑定change事件:

var code = "123";
$("#mySelect").on("change", function() {
    code = this.value;
    reload_script();
});

否则,在纯JavaScript中它可能如下所示:

var code = "123";
document.getElementById("mySelect").onchange = function() {
    code = this.value;
    reload_script();
};

答案 1 :(得分:1)

试试这个

<select onchange="reload_script();">
  <option value="123">Select 123</option>
  <option value="456">Select 456</option>
<select>

<div id="123">
    <a href="http://same.com/index.do?n=152991">link title</a>
    <br/><br/>
    <a href="http://some.com/">Link</a>
</div>
<script language="javascript" type="text/javascript">
var code = '123' // <-- THIS VARIABLE WOULD BE THE VALUE OF THE SELECTED DROPDOWN VALUE
function script_to_run()
{
//do code here
}

// Call the script one time on page load (only if you need it on page load)
script_to_run();

function reload_script()
{
  script_to_run();//run again
}


</script>