Looping through js array and variable assignment doesn't work
I have a price of js code, which has been troubling me since last few days, below is the piece of code, the problem is during the handling of the ajax response. The strange thing about this code is it works fine on my local machine, but does not work correctly in the online server, I have commented the issues by the code.
The strange thing about this code is , when I put it up online, the currField
and the tempfieldDivId
variables don't work at all and the temp
variable(which I added for debugging) is printed in the alert with the correct value. But it fail开发者_StackOverflows in the switch case.
And in the switch it always falls to the the default option and prints "No match" .
$.ajax({
type: "POST",
url: "components/form/form_processing.php",
data: dataStringfrmFlds,
cache:false,
success: function(opt){
alert(opt);// opt is fine, returns: ship_to,first_notify_party,consignee,shipment_type,packaging_requirements,testing_requirements,date,request_expected_date,order_ponunber
var defaultFields = opt.split(',');
var numFields = defaultFields.length;
for(i = 0; i < numFields; i++){
var temp = "";
var currField = defaultFields[i];
var tempfieldDivId = "def_"+currField;
//these doesnt work
$(tempfieldDivId).show();
$('#'+currField).attr('required','required');
temp = currField;
alert(temp); //this does print the field name in the alert
//these doesnt work either
switch(temp){
case "os_id":
$("#def_os_id").show();
break;
case "ff_id":
$("#def_ff_id").show();
break;
case "fnp_id":
alert("HI there");
$("#def_fnp_id").show();
break;
case "cinfo_id":
$("#def_cinfo_id").show();
break;
case "order_date":
$("#def_order_date").show();
break;
case "order_shiptype":
$("#def_order_shiptype").show();
break;
case "order_portdis":
$("#def_order_portdis").show();
break;
case "order_requestform":
$("#def_order_requestform").show();
break;
case "order_specialinstructions":
$("#def_order_specialinstructions").show();
break;
case "order_packreq":
$("#def_order_packreq").show();
break;
case "order_testreq":
$("#def_order_testreq").show();
break;
case "order_reqexdate":
$("#def_order_testreq").show();
break;
default:
alert("No match");//this is printed out all the times
break;
}//end of switch
}//end of for loop
}
});
I would be very grateful if anyone can help me on this. Thank you.
$(tempfieldDivId).show();
do you need to add a "#" before tempfieldDivId?-
opt
isship_to,first_notify_party,consignee,shipment_type,packaging_requirements,testing_requirements,date,request_expected_date,order_ponunber
I didn't see any of them match any case in yourswitch
statement, of course it will fall down to default every time.
精彩评论