Jquery $j.ajax()成功回调方法未按预期创建新表行
我有一个屏幕,用户可以点击“添加装运点”按钮,弹出一个对话框,输入信息,点击“添加”,这会发出一个Ajax调用,将装运点添加到db中,然后关闭对话框,Ajax调用的成功回调方法应该将tr追加到装运点表中。除了没有添加tr外,其他一切正常 下面是shipping points表的html,该表应将该行添加到该表中Jquery $j.ajax()成功回调方法未按预期创建新表行,jquery,ajax,Jquery,Ajax,我有一个屏幕,用户可以点击“添加装运点”按钮,弹出一个对话框,输入信息,点击“添加”,这会发出一个Ajax调用,将装运点添加到db中,然后关闭对话框,Ajax调用的成功回调方法应该将tr追加到装运点表中。除了没有添加tr外,其他一切正常 下面是shipping points表的html,该表应将该行添加到该表中 <table id="shipPoints" class="ui-widget-content" width="697"> <thead> <tr
<table id="shipPoints" class="ui-widget-content" width="697">
<thead>
<tr class="ui-widget-content" width="696">
<th class="ui-widget-header" width="395">
Shipping Points
</th>
<th class="ui-widget-header" width="300">
Remove
</th>
</tr>
</thead>
<tbody>
<c:forEach items="${shippingPoints}" var="shippingPoint">
<tr width="695">
<td with="395">
${shippingPoint.shippingPointsCity},
${shippingPoint.shippingPointsState}
</td>
<td width="300">
<INPUT type="checkbox" NAME="chk" value="${shippingPoint.shippingPointsId}" />
<INPUT type="hidden" NAME="shipPointId" VALUE="${shippingPoint.shippingPointsId}" />
</td>
</tr>
</c:forEach>
</tbody>
</table>
装运点
去除
${shippingPoint.shippingPointsCity},
${shippingPoint.shippingPointsState}
下面是正在进行这项工作的jquery
function saveShippingPoint()
{
//alert("Before ajax call.");
$j.ajax(
{
url: "<portlet:resourceURL id='saveShippingPoint'/>" +
"?city=" + $j( "#city" ).val().toUpperCase() +
"&state=" + $j( "#state" ).val().toUpperCase() +
"&stateOther=" + $j( "#stateOther" ).val().toUpperCase() +
"&zip=" + $j( "#zip" ).val() +
"&product=" + $j( "#product" ).val().toUpperCase() ,
type: 'GET',
cache: false,
timeout: 30000,
success: function(data)
{
//alert("In success callback.");
$j("#shipPoints tr:last").after(
"<tr>"
+ "<td>"
+ city.val().toUpperCase()
+ ", "
+ state.val().toUpperCase()
+ "</td>"
+ "<td>"
+ "<INPUT type='checkbox' NAME='chk' VALUE='"+ data + "' />"
+ "<INPUT type='hidden' NAME='shipPointId' VALUE='"+ data + "' />"
+ "</td>"
+ "</tr>");
},
error: function()
{
alert("There was a problem adding the shipping point.");
}
});
//alert("Done with ajax call, about to return.");
return;
};
函数saveShippingPoint()
{
//警报(“在ajax调用之前”);
$j.ajax(
{
url:“+
“?city=“+$j(“#city”).val().toUpperCase()+
“&state=“+$j(#state”).val().toUpperCase()+
“&stateOther=“+$j”(#stateOther”).val().toUpperCase()+
“&zip=“+$j(#zip”).val()+
“&product=“+$j(“#product”).val().toUpperCase(),
键入:“GET”,
cache:false,
超时:30000,
成功:功能(数据)
{
//警报(“在成功回调中”);
$j(“#shipPoints tr:last”)。在(
""
+ ""
+city.val().toUpperCase()
+ ", "
+state.val()
+ ""
+ ""
+ ""
+ ""
+ ""
+ "");
},
错误:函数()
{
警报(“添加装运点时出现问题”);
}
});
//警报(“使用ajax调用完成,即将返回”);
返回;
};
以下是用于输入信息的对话框的代码
<div id="dialog-form" title="Shipping Points">
<p class="validateTips">
Please include all vendor ship points by product group. If vendor
ships all products from one location input City, State, Zip Code
then select "All" for product group.
</p>
<fieldset>
<label font-family="Courier New" align="left" for="city">City</label>
<input maxlength=50 align="right" type="text" name="city" id="city"
class="text ui-corner-all" />
<br />
<label font-family="Courier New" align="left" for="state">State</label>
<select maxlength=6 align="right" name="state" id="state"
class="text ui-corner-all">
<option value="">Select State...</option>
<c:forEach items="${states}" var="state">
<option value="${state.fieldValue}">
${state.fieldDescription}
</option>
</c:forEach>
</select>
<br />
<label font-family="Courier New" align="left" for="stateOther">State (Other):</label>
<input maxlength=6 align="right" type="text" name="stateOther" id="stateOther" value=""
class="text ui-corner-all" />
<br />
<label font-family="Courier New" align="left" for="zip">Zip</label>
<input align="right" maxlength=10 align="right" type="text" name="zip" id="zip" value=""
class="text ui-corner-all" />
<br />
<label font-family="Courier New" align="left" align="left" for="product">Product</label>
<input align="right" maxlength=50 type="text" name="product" id="product" value=""
class="text ui-corner-all" />
<br />
</fieldset>
</div>
请按产品组列出所有供应商发货点。如果供应商
从一个地点发货所有产品输入城市、州、邮政编码
然后为产品组选择“全部”。
城市
陈述
选择状态。。。
${state.fieldDescription}
国家(其他):
拉链
产品
函数saveShippingPoint(){
$j.ajax({
url:“urlpath”,
数据:{city:$j(“#city”).val().toUpperCase(),
state:$j(“#state”).val().toUpperCase(),
stateOther:$j(“#stateOther”).val().toUpperCase(),
zip:$j(“#zip”).val(),
产品:$j(“#产品”).val().toUpperCase()
},
键入:“GET”,
cache:false,
超时:30000,
成功:功能(数据){
警报(JSON.stringify(数据));
var htmlTr=“+city.val().toUpperCase()+”,“+state.val().toUpperCase()+”
+ "";
警报(htmlTr);
//或
var htmlTr=“+$j('#city').val().toUpperCase()+”,“+$j('#state').val().toUpperCase()+”
+ "";
警报(htmlTr);
$j(“#装运点”).find('tbody tr:last')。after(htmlTr);
//或
$j(“#shipPoints”).find('tbody').append(htmlTr);
},
错误:函数(){
警报(“添加装运点时出现问题”);
}
});
}
好的,我让它工作了
对一些代码进行了无序处理,将函数内联移动等,但这并没有起到任何作用。正在进行成功调用,我正在调用中获取id,但没有创建行,并且没有关闭对话框。修改了对话框“关闭”上的选择器并使其正常工作。所以我想可能是桌子上的选择器出了问题
我从tr.after()调用中提取html并将其放入变量中,这样我就可以在after()调用之前将其放入警报中,这样我就可以保证传递的是有效的html。。。出于某种原因,这解决了问题
这是工作版本
$j("#dialog-form").dialog(
{
autoOpen : false,
height : 500,
width : 500,
modal : true,
buttons :
{
"Add Shipping Point" : function()
{
var bValid = true;
var cityValid = true;
var stateValid = true;
var zipPresent = true;
var zipValid = true;
updateTips("");
allFields.removeClass("ui-state-error");
cityValid = checkRequired(city, "City");
stateValid = checkRequired(state, "State");
zipPresent = checkRequired(zip, "Zip");
if(zipPresent) { zipValid = checkRegexp(zip, /(^\d{5}$)|(^\d{5}-\d{4}$)/, "Zip Code"); }
bValid = cityValid && stateValid && zipPresent && zipValid;
if (bValid)
{
//alert("Before save shipping point.");
function saveShippingPoint()
{
//alert("Before ajax call.");
$j.ajax(
{
url: "<portlet:resourceURL id='saveShippingPoint'/>" +
"?city=" + $j( "#city" ).val().toUpperCase() +
"&state=" + $j( "#state" ).val().toUpperCase() +
"&stateOther=" + $j( "#stateOther" ).val().toUpperCase() +
"&zip=" + $j( "#zip" ).val() +
"&product=" + $j( "#product" ).val().toUpperCase() ,
type: 'GET',
cache: false,
timeout: 30000,
success: function(data)
{
var row = "<tr>"
+ "<td>"
+ city.val().toUpperCase()
+ ", "
+ state.val().toUpperCase()
+ "</td>"
+ "<td>"
+ "<INPUT type='checkbox' NAME='chk' VALUE='"+ data + "' />"
+ "<INPUT type='hidden' NAME='shipPointId' VALUE='"+ data + "' />"
+ "</td>"
+ "</tr>";
//alert("In success callback. About to add row " + row);
//$j("#shipPoints').find('tbody tr:last").after(
$j("#shipPoints tr:last").after(row);
//alert("After ajax call. About to call close");
$j("#dialog-form").dialog("close");
},
error: function()
{
alert("There was a problem adding the shipping point.");
}
});
};
saveShippingPoint();
}
},
Cancel : function()
{
$j(this).dialog("close");
}
},
close : function()
{
allFields.val("").removeClass("ui-state-error");
}
});
$j(“#对话框形式”)。对话框(
{
自动打开:错误,
身高:500,
宽度:500,
莫代尔:是的,
按钮:
{
“添加装运点”:函数()
{
var bValid=真;
var cityValid=true;
var statefalid=true;
var-zipPresent=true;
var-zipValid=true;
updateTips(“”);
removeClass(“ui状态错误”);
cityValid=需要检查(城市,“城市”);
stateValid=checkRequired(状态,“状态”);
zipPresent=checkRequired(zip,“zip”);
如果(zipPresent){zipValid=checkRegexp(zip,/(^\d{5}$)|(^\d{5}-\d{4}$)/,“邮政编码”);}
bValid=cityValid&&stateValid&&zipPresent&&zipValid;
if(bValid)
{
//警报(“保存装运点之前”);
函数saveShippingPoint()
{
//警报(“在ajax调用之前”);
$j.ajax(
{
url:“+
“?city=“+$j(“#city”).val().toUpperCase()+
“&state=“+$j(#state”).val().toUpperCase()+
“&stateOther=“+$j”(#stateOther”).val().toUpperCase()+
“&zip=“+$j(#zip”).val()+
“&product=“+$j(“#product”).val().toUpperCase(),
键入:“GET”,
cache:false,
超时:30000,
成功:功能(数据)
{
$j("#dialog-form").dialog(
{
autoOpen : false,
height : 500,
width : 500,
modal : true,
buttons :
{
"Add Shipping Point" : function()
{
var bValid = true;
var cityValid = true;
var stateValid = true;
var zipPresent = true;
var zipValid = true;
updateTips("");
allFields.removeClass("ui-state-error");
cityValid = checkRequired(city, "City");
stateValid = checkRequired(state, "State");
zipPresent = checkRequired(zip, "Zip");
if(zipPresent) { zipValid = checkRegexp(zip, /(^\d{5}$)|(^\d{5}-\d{4}$)/, "Zip Code"); }
bValid = cityValid && stateValid && zipPresent && zipValid;
if (bValid)
{
//alert("Before save shipping point.");
function saveShippingPoint()
{
//alert("Before ajax call.");
$j.ajax(
{
url: "<portlet:resourceURL id='saveShippingPoint'/>" +
"?city=" + $j( "#city" ).val().toUpperCase() +
"&state=" + $j( "#state" ).val().toUpperCase() +
"&stateOther=" + $j( "#stateOther" ).val().toUpperCase() +
"&zip=" + $j( "#zip" ).val() +
"&product=" + $j( "#product" ).val().toUpperCase() ,
type: 'GET',
cache: false,
timeout: 30000,
success: function(data)
{
var row = "<tr>"
+ "<td>"
+ city.val().toUpperCase()
+ ", "
+ state.val().toUpperCase()
+ "</td>"
+ "<td>"
+ "<INPUT type='checkbox' NAME='chk' VALUE='"+ data + "' />"
+ "<INPUT type='hidden' NAME='shipPointId' VALUE='"+ data + "' />"
+ "</td>"
+ "</tr>";
//alert("In success callback. About to add row " + row);
//$j("#shipPoints').find('tbody tr:last").after(
$j("#shipPoints tr:last").after(row);
//alert("After ajax call. About to call close");
$j("#dialog-form").dialog("close");
},
error: function()
{
alert("There was a problem adding the shipping point.");
}
});
};
saveShippingPoint();
}
},
Cancel : function()
{
$j(this).dialog("close");
}
},
close : function()
{
allFields.val("").removeClass("ui-state-error");
}
});