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 is then selecting the correct option.
Any suggestions?
Lance
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>