我有一个弹出式notifyDa,弹出窗口显示3个不同的条件.. 这工作正常。我面临的问题是验证。 验证方法不起作用。 除了“必需”字段验证之外,我还有一个场景,其中有2个下拉列表,如果选择了其中一个,则可以提交表单..
我的弹出代码是
function notifyDa(daValueForUser) {
var ctx = '${contextPath}';
var queryUrl = ctx + "/excessList.htm?getDaList=true";
var html = "<form id='notifyForm'><table><tr><td align='center' colspan='2'> Excess Notification </td></tr>";
$.ajax({
url : queryUrl,
type : "POST",
dataType : "text",
success : function(result) {
result = jQuery.parseJSON(result); // as dataType is Text
var items = result;
if(daValueForUser>=4) {
html = html + "<tr><td>Select DA Holder</td><td><select id='daList'><option>select</option>";
for ( var i = 0; i < items.length; i++ ) {
html = html + "<option value='"+items[i]+"'>"+items[i]+"</option>";
}
}
if (daValueForUser<=3) {
html = html + "<tr><td>Select DA4 Holder</td><td><select id='daList1'>";
for ( var i = 0; i < items.length; i++) {
html = html + "<option value='"+items[i]+"'>"
+ items[i] + "</option>";
}
html = html + "</select></td></tr><tr><td>Select DA4 Holder</td><td><select id='daList2'>";
for ( var i = 0; i < items.length; i++) {
html = html + "<option value='"+items[i]+"'>"
+ items[i] + "</option>";
}
}
html = html+"</select></td></tr><tr><td>Comments:</td><td><input name='comments'></td></tr>" +
"<tr><td></td><td> <table><tr><td align='right'><button type='button' onclick='javascript:sendNotifyDetails();'>Send</button></td><td align='left'><button type='button' onclick='javascript:closeNotifyDaPopup();'>Cancel</button></td></tr></table></td></tr></table></form>";
$('#notifyDiv').empty();
$('#notifyDiv').html(html);
$("#pop").click(function() {
$("#notifyDiv").fadeIn(1000);
if (!$("#notifyDiv").is(':visible')) {
return;
}
});
$("#notifyDiv").css({
left : ($(window).width() - $('#notifyDiv').width()) / 2,
top : ($(window).width() - $('#notifyDiv').width()) / 7,
position : 'absolute'
});
$('#daList1').change(function(e) {
index = this.selectedIndex;
$('#daList2 option').prop('disabled', false); // enable all options
if (index != 0) {
$('#daList2').get(0).selectedIndex = 0; // so you don't disable the option after it's selected
$('#daList2 option:eq(' + index + ')').prop('disabled', true);
}
});
},
error : function() {
}
});
}
我的验证代码是
function sendNotifyDetails(){
alert("Inside close");
$("#notifyForm").validate({
submitHandler: function() { alert("Submitted!");},
rules: {
daList: "required",
comments: "required"
},
errorElement: "div",
messages: {
daList: "Select a DA Holder",
comments: "Enter comments"
}
});
}
正在输入验证方法但不显示错误消息。 我给出了span元素的错误元素,并在选项之前插入。 现在我将代码更改为
function notifyDa(daValueForUser) {
var ctx = '${contextPath}';
var queryUrl = ctx + "/excessList.htm?getDaList=true";
//daValueForUser=3
var html = "<form id='notifyForm'><span class='errorMessage'></span><div id='error'></div><table><tr><td align='center' colspan='2'> Excess Notification </td></tr>";
$.ajax({
url : queryUrl,
type : "POST",
dataType : "text",
success : function(result) {
result = jQuery.parseJSON(result); // as dataType is Text
var items = result;
if(daValueForUser>=4) {
html = html + "<tr><td>Select DA Holder</td><td><select id='daList'><option>select</option>";
for ( var i = 0; i < items.length; i++ ) {
html = html + "<option value='"+items[i]+"'>"+items[i]+"</option>";
}
}
if (daValueForUser<=3) {
html = html + "<tr><td>Select DA4 Holder</td><td><select id='daList1'>";
for ( var i = 0; i < items.length; i++) {
html = html + "<option value='"+items[i]+"'>"
+ items[i] + "</option>";
}
html = html + "</select></td></tr><tr><td>Select DA4 Holder</td><td><select id='daList2'>";
for ( var i = 0; i < items.length; i++) {
html = html + "<option value='"+items[i]+"'>"
+ items[i] + "</option>";
}
}
html = html+"</select></td></tr><tr><td>Comments:</td><td><input name='comments'></td></tr>" +
"<tr><td></td><td> <table><tr><td align='right'><input type='submit' value='Send'/></td><td align='left'><button type='button' onclick='javascript:closeNotifyDaPopup();'>Cancel</button></td></tr></table></td></tr></table></form>";
$('#notifyDiv').empty();
$('#notifyDiv').html(html);
$("#notifyForm").validate({
submitHandler: function() { alert("Submitted!");},
errorElement: "span",
errorPlacement: function(error, element) {
error.insertBefore($("#error"));
},
rules: {
daList: "required",
comments: "required"
},
messages: {
daList: "Enter DA Holder",
comments: "comments required"
}
});
$("#notifyDiv").fadeIn(1000);
$("#notifyDiv").css({
left : ($(window).width() - $('#notifyDiv').width()) / 2,
top : ($(window).width() - $('#notifyDiv').width()) / 7,
position : 'absolute'
});
$('#daList1').change(function(e) {
index = this.selectedIndex;
$('#daList2 option').prop('disabled', false); // enable all options
if (index != 0) {
$('#daList2').get(0).selectedIndex = 0; // so you don't disable the option after it's selected
$('#daList2 option:eq(' + index + ')').prop('disabled', true);
}
});
},
error : function() {
}
});
}
答案 0 :(得分:2)
您需要在关闭事件发生前调用$('#notifyForm').validate
。
因此,只要您使用此代码创建表单:
$('#notifyDiv').html(html);
之后就是你应该调用的地方:
$("#notifyForm").validate({
submitHandler: function() { alert("Submitted!");},
rules: {
daList: "required",
comments: "required"
},
errorElement: "div",
messages: {
daList: "Select a DA Holder",
comments: "Enter comments"
}
});
然后,将您的按钮更改为这样的提交按钮:
旧:
<button type='button' onclick='javascript:sendNotifyDetails();'>Send</button>
新:
<input type="submit" value="Send"/>
这将自动触发验证,如果表单有效,则会调用submitHandler。