提交表单后如何防止选择选项重置值

时间:2018-07-09 03:37:16

标签: javascript php sql

<form action="mydtr" method="post">

<select id="periodname">
<?php foreach($period as $period): ?>

<option><?= $period['period']; ?></option>
<?php endforeach; ?>
</select>
</form>

我正在寻找防止选择和提交后重置选择选项的方法。预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

不确定这是否是您要的东西,但是就走了。

<?php $period = array("period1", "period2", "period3", "period4"); ?>

<form action="" method="post">
<select id="periodname" name="periodname">
    <?php foreach($period as $period): ?>
        <option><?= $period; ?></option>
    <?php endforeach; ?>
</select>
<input value="submit" type="submit"></form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<?php if(isset($_POST["periodname"])): echo "Form was submitted and we set that value of the select box to selected ".$_POST["periodname"];?>
<script>
    $( "#periodname option" ).each(function( index ) {
        var item = $(this).val();
      if(item == "<?php echo $_POST["periodname"]?>"){
          $(this).prop('selected', true);
      }
    });
</script>
<?endif;?>