选择表单更改,弹出警报然后发送表单

时间:2017-11-02 08:47:12

标签: jquery forms select onchange

我使用了Greg Hornby在这个链接上给出的答案" When html select form changes, pop up alert then send form" 它工作得很好但是对于一个问题,即使我们按下取消按钮,选择框仍然会改变,它不会保存更改但是如果我们刷新页面它确实显示已更改但是我们更改选择框时它会显示更改节目变了, 我的问题是,如果我们按取消

,我们如何阻止选择框更改

我的代码是



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form method="post" action="">
<select name="payment_status" class="form-control status_select" onchange="if(confirm('Are you sure you want to Change this Status?')){this.form.submit()}">
	<option value="">Select Payment Status</option>
	<option value="Un paid">Un-paid</option>
	<option value="Paid">Paid</option>
</select>
</form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

使用prop()将所选选项设置为false语句中的else

&#13;
&#13;
$("select").change(function(){
    if(confirm('Are you sure you want to Change this Status?')){
        {this.form.submit()} 
    }
    else $("select option:selected").prop("selected", false);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form method="post" action="">
<select name="payment_status" class="form-control status_select">
	<option value="">Select Payment Status</option>
	<option value="Un paid">Un-paid</option>
	<option value="Paid">Paid</option>
</select>
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

例如,您可以将初始状态设置为变量,然后在确认更改时更新该变量。取消时,将值重置为先前确认或初始状态,如下所示:

<script>
var initial = '<?php echo "Paid"; ?>'; 
function reset(){ $("select").val(initial); }
</script>

和onChange如果确认,则提交,否则重置()