我正在使用 jquery 在 Alt+S 上保存一个表单,并且我还在检查必填字段。它工作正常,但我有一个问题。当我在输入必填字段之前按 Alt+S 时,它会给我必填字段的错误,而当我在必填字段中键入任何内容时,表单就会被保存。我希望在填写字段时出错并且如果用户再次按 Alt+S 则应该保存表单。
jQuery代码
$(document).ready(function () {
var isAltKey = false;
var isShiftKey = false;
document.onkeyup = function (e) {
if (e.which == 18) isAltKey = false;
if (e.which == 16) isShiftKey = false;
}
document.onkeydown = function (e) {
if (e.which == 18) isAltKey = true;
if (e.which == 16) isShiftKey = true;
//ALT+S
if (e.which == 83 && isAltKey == true) {
TriggerSaveButton();
StopDefaultAction(e);
}
else if (e.which == 9 && isShiftKey == true) {
if (document.getElementById("cmbUnder_OptionList").focus() == true) {
document.getElementById("txtGroupSname").focus();
StopDefaultAction(e);
}
}
}
function StopDefaultAction(e) {
if (e.preventDefault) { e.preventDefault() }
else { e.stop() };
e.returnValue = false;
e.stopPropagation();
}
function TriggerSaveButton() {
var groupname = $('#txtGroupName').val();
if ($('#tab2').is(":visible")) {
if ((document.getElementById('<%= txtGroupName.ClientID %>').value == "") && ($('<%= listboxdestination.ClientID %>').children().length == 0)) {
ValidatorEnable(document.getElementById('<%= reqtxtGroupName.ClientID %>'), true);
ValidatorEnable(document.getElementById('<%= reqlstboxdest.ClientID %>'), true);
return false;
}
else if (groupname != "" && ($("#<%= listboxdestination.ClientID %> option").length != 0)) {
javascript: __doPostBack('<%=btnaddfrmSave.UniqueID %>', '');
}
}
else {
if ((document.getElementById('<%= txtGroupName.ClientID %>').value == "")) {
ValidatorEnable(document.getElementById('<%= reqtxtGroupName.ClientID %>'), true);
return false;
}
else if (groupname != "") {
javascript: __doPostBack('<%=btnaddfrmSave.UniqueID %>', '');
}
}
}
});