简体   繁体   English

如何在 Javascript 上添加 onChange 事件以仅重新加载选择选项

[英]How to add onChange event on Javascript to reload only the select option

HTML HTML

<form method="post" name="f1" action='dd-check.php'>
  <select  class="text" id="course_id" name='course_id' onChange="reload(this.form)">
    <option value=''>Select one</option>
    <option value=''><?php ?></option>
  </select>
    
  <select name="batch_id" id="batch_id">
    <option value=''>Select one</option>
    <option value=''><?php ?></option>
  </select>
</form>

Javascript Javascript

<script type="text/javascript">
  function reload(form)
  {
    var val = form.course_id.options[form.course_id.options.selectedIndex].value;
    self.location='dd.php?course_id=' + val ;
  }
</script>

In here the function reload the form.在这里,函数重新加载表单。 I want to reload only the first select box data, how to do that?我只想重新加载第一个选择框数据,该怎么做?

onchange() event depends on its element, thus reload() does not adjust the second select element "batch_id" onchange() 事件取决于它的元素,因此 reload() 不会调整第二个选择元素“batch_id”

Additionally, before changing the location, add parameter selected index of two option elements.此外,在更改位置之前,添加两个选项元素的参数选择索引。

    var s1 = document.getElementById("course_id");
    var o1 = s1.options.selectedIndex;

    var s2 = document.getElementById("batch_id");
    var o2 = s2.options.selectedIndex;

And then, in onload function you restore the option values selected before.然后,在 onload 功能中,您可以恢复之前选择的选项值。

    var s1 = document.getElementById("course_id");
    s1.options.selectedIndex = o1;
    var s2 = document.getElementById("batch_id");
    s2.options.selectedIndex = o2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM