0

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

这是我当前的代码示例:

<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' ...。这就是更改选择框时更改的内容...然后我需要重新运行脚本...

这可能吗?

4

2 回答 2

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();
};
于 2012-05-11T10:47:02.423 回答
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>
于 2012-05-11T10:48:11.967 回答