我有一个表单,每个客户请求都有多个保存按钮以及一些其他按钮。单击保存按钮时,将打开确认框。如果他们选择取消,则意图是阻止表单提交。但是,当用户尝试选择除保存按钮以外的保存按钮时,他们只是按下没有任何反应。
$("form").submit(function (e) {
var $btn = $(document.activeElement);
if ($btn.attr("value") == "Save") {
var c = confirm("Did you review the suicide related issues, signs and factors for currency?");
if (!c) {
$btn.css('filter', 'none');
e.preventDefault();
} else {
return true;
}
}
});
答案 0 :(得分:0)
我的代码存在一些问题:
1-使用activeElement似乎dicey,我会切换到target
id。
澄清这一点:从这个答案:Just a note to keep things current. As of right now, Safari sets activeElement to the document body when clicking on a submit button. I ran into this issue on Safari 9.1
2-检查按钮后是保存,提示确认,然后调用.submit(处理程序)等
我无法在电脑上重现您的行为,在Mac上有bugs所以它甚至无法运行
编辑:
以下是我的建议:
$(":button").click(function(e) {
if (e.target.value == "Save") {
var c = confirm("Did you review the suicide related issues, signs and factors for currency?");
if (!c) {
console.log('form not submited');
} else {
$("form").submit(function() {
console.log('Form Submited');
});
}
}
});
HTML:
<form>
<button value ="Save" type="submit" name="button_1">Button #1</button>
<button value ="Save" type="submit" name="button_2">Button #2</button>
<button value ="Save" type="submit" name="button_3">Button #3</button>
<button value ="Save" type="submit" name="button_4">Button #4</button>
<button value ="Save" type="submit" name="button_5">Button #5</button>
<button value ="Save" type="submit" name="button_6">Button #6</button>
</form>