本文介绍了提交表单后保留选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form method =getaction => 
< select name =name>
< option value =a> a< / option>
< option value =b> b< / option>
< / select>
< select name =location>
< option value =x> x< / option>
< option value =y> y< / option>
< / select>
< input type =submitvalue =Submitclass =submit/>
< / form>

提交表单时,如何确保所选值在下拉菜单中保持选中状态?为避免许多if-else结构,让javascript自动执行该操作: / p>

 < select name =nameid =name> 
< option value =a> a< / option>
< option value =b> b< / option>
< / select>

< script type =text / javascript>
document.getElementById('name')。value =<?php echo $ _GET ['name'];?>;
< / script>

< option value =x> x< / option>
< option value =y> y< / option>
< / select>

< script type =text / javascript>
document.getElementById('location')。value =<?php echo $ _GET ['location'];?>;
< / script>


<form method="get" action="">
   <select name="name">
      <option value="a">a</option>
      <option value="b">b</option>
   </select>
   <select name="location">
      <option value="x">x</option>
      <option value="y">y</option>
   </select>
   <input type="submit" value="Submit" class="submit" />
</form>

On submitting the form, how do I make sure that the selected values remain selected in the dropdowns? This form is inside wordpress (PHP).

解决方案

To avoid many if-else structures, let javascript do the trick automatically:

 <select name="name" id="name">
  <option value="a">a</option>
  <option value="b">b</option>
 </select>

<script type="text/javascript">
  document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>

 <select name="location" id="location">
  <option value="x">x</option>
  <option value="y">y</option>
 </select>

<script type="text/javascript">
  document.getElementById('location').value = "<?php echo $_GET['location'];?>";
</script>

这篇关于提交表单后保留选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:27
查看更多