选择“选择选项”使用 AJAX 创建后?
我有一个页面,其中有一系列“相关”选择。一切正常,除非有一个预先选择的选项。如果我在代码中放置“警报”,我可以将“预选”设置为工作,但如果没有它,它就不起作用。
例如:
function loader(){
if ($("#prod_1").val() > 0){
switchBatch(1);
$('#batch_1').val('15');
updateMax(1);
}
if ($("#prod_2").val() > 0){
switchBatch(2);
alert('yup');
$('#batch_2').val('35');
updateMax(2);
}
}
$(function() {
loader();
});
第二个具有“alert('yup');”它有效,但第一个无效。
“switchBatch()”是一个将选项(来自 ajax 调用)加载到批量选择控件中的函数。两个实例都会加载选项,但只有第二个实例选择正确的选项。
有什么建议吗?
这是整个事情:
<script>
maxVals = [];
function switchBatch(idNum){
maxVals = [];
$("#max_"+idNum).val('');
$.ajax({
type: "POST",
url: "/cfc/product.cfc?method=pialJson",
data: ({
productID: $("#prod_"+idNum).val()
}),
dataType: "json",
success: function(result){
options = '';
var colMap = new Object();
for(var i = 0; i < result.COLUMNS.length; i++) {
colMap[result.COLUMNS[i]] = i;
}
for (var i = 0; i < result.DATA.length; i++){
options += '<option value="' + result.DATA[i][colMap["BATCHID"]] + '">' + result.DATA[i][colMap["BATCHNAME"]]+ '</option>';
maxVals[i] = result.DATA[i][colMap["INSTOCK"]];
}
$("select#batch_"+idNum).html(options);
updateMax(idNum);
}
});
}
function updateMax(idNum){
thisOne = $("#batch_"+idNum).attr("selectedIndex");
$("#max_"+idNum).val(maxVals[thisOne]);
checkMax(idNum);
}
function checkMax(idNum){
$("#qty_"+idNum).removeClass('red');
if ($("#qty_"+idNum).val() > $("#max_"+idNum).val()){
$("#qty_"+idNum).addClass('red');
}
}
function loader(){
if ($("#prod_1").val() > 0){
switchBatch(1);
alert('yup');
$('#batch_1').val('<cfoutput>#batch_1#</cfoutput>');
updateMax(1);
}
if ($("#prod_2").val() > 0){
switchBatch(2);
alert('yup');
$('#batch_2').val('<cfoutput>#batch_2#</cfoutput>');
updateMax(2);
}
}
$(function() {
loader();
});
</script>
I have a page that has a series of "related" selects. Everything works fine UNLESS there is an option that is pre-selected. I can set the "pre-selection" to work if I put an "alert" in the code but without it, it doesn't work.
For Example:
function loader(){
if ($("#prod_1").val() > 0){
switchBatch(1);
$('#batch_1').val('15');
updateMax(1);
}
if ($("#prod_2").val() > 0){
switchBatch(2);
alert('yup');
$('#batch_2').val('35');
updateMax(2);
}
}
$(function() {
loader();
});
The second one that has the "alert('yup');" in it works but the first one doesn't.
The "switchBatch()" is a function that loads the options (from an ajax call) into the batch select control. Both instances load the options but only the second one selects the correct option.
Any suggestions?
Here is the whole thing:
<script>
maxVals = [];
function switchBatch(idNum){
maxVals = [];
$("#max_"+idNum).val('');
$.ajax({
type: "POST",
url: "/cfc/product.cfc?method=pialJson",
data: ({
productID: $("#prod_"+idNum).val()
}),
dataType: "json",
success: function(result){
options = '';
var colMap = new Object();
for(var i = 0; i < result.COLUMNS.length; i++) {
colMap[result.COLUMNS[i]] = i;
}
for (var i = 0; i < result.DATA.length; i++){
options += '<option value="' + result.DATA[i][colMap["BATCHID"]] + '">' + result.DATA[i][colMap["BATCHNAME"]]+ '</option>';
maxVals[i] = result.DATA[i][colMap["INSTOCK"]];
}
$("select#batch_"+idNum).html(options);
updateMax(idNum);
}
});
}
function updateMax(idNum){
thisOne = $("#batch_"+idNum).attr("selectedIndex");
$("#max_"+idNum).val(maxVals[thisOne]);
checkMax(idNum);
}
function checkMax(idNum){
$("#qty_"+idNum).removeClass('red');
if ($("#qty_"+idNum).val() > $("#max_"+idNum).val()){
$("#qty_"+idNum).addClass('red');
}
}
function loader(){
if ($("#prod_1").val() > 0){
switchBatch(1);
alert('yup');
$('#batch_1').val('<cfoutput>#batch_1#</cfoutput>');
updateMax(1);
}
if ($("#prod_2").val() > 0){
switchBatch(2);
alert('yup');
$('#batch_2').val('<cfoutput>#batch_2#</cfoutput>');
updateMax(2);
}
}
$(function() {
loader();
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为预选择不起作用,因为“switchBatch()”函数使用ajax。调用“switchBatch()”之后的 JavaScript 代码在 ajax 调用完成之前执行,因此 select 元素为空。但由于alert(),它在第二个 if 块中起作用,它为 ajax 调用提供了足够的时间来完成并填充 select 元素。
I think the pre-selects aren't working because the "switchBatch()" function uses ajax. The JavaScript code after the call to "switchBatch()" is executed before the ajax call is complete, so the select elements are empty. But it works in the second if-block because of the alert(), which gives the ajax call enough time to complete and populate the select element.
让您的
switchBatch()
函数接受另一个参数,该参数是一个包含您在 AJAX 请求后尝试运行的代码的函数。然后在
success:
回调中为switchBatch() 中的 AJAX 请求调用该函数。
Have your
switchBatch()
function accept another argument, which is a function containing the code that you are trying to run after the AJAX request.Then call that function in the
success:
callback for the AJAX request inswitchBatch().
如果没有
updateMax()
代码,很难说到底发生了什么。您可以尝试的一种方法是将
updateMax()
附加到选择控件的onchange
侦听器(即$('#selectID').change(updateMax)
),而不是调用updateMax()
,而是执行$('#selectID').change()
。Without the code of
updateMax()
, it's hard to say what's exactly going on.One approach you can try is, attach
updateMax()
to theonchange
listener of the select control (i.e.$('#selectID').change(updateMax)
), and instead of callingupdateMax()
, do$('#selectID').change()
.