我有一个包含文本框和选择框的表单。我正在向PHP页面发送和Ajax请求,并且我正在根据该请求自动填写此表单。填充文本框没有问题,但如何确定所选的选择框选项?任何帮助都会得到满足。
$(document).ready(function(){
$("#companyFillSelectBox").change(function(){
var value = $("select#companyFillSelectBox option:selected").val();
$.ajax({
type: 'POST',
url: 'companyFill.php',
dataType: "json",
data:{companyID:value},
success:function(answer){
$('#companyNameText').val(answer[0].companyName);
$('#companyAddressTextArea').val(answer[0].companyAddress);
$('#companyNetAdressText').val(answer[0].companyWebAddress);
$('#companyPhoneText').val(answer[0].companyPhone);
$('#companyFaxText').val(answer[0].companyFax);
$('#companyCeoText').val(answer[0].companyCeo);
$('#companyHRText').val(answer[0].hrDirector);
$('#numOfIengText').val(answer[0].numOfIENG);
$('#numOfEngText').val(answer[0].numOfENG);
$('#numOfWhiteCollarsText').val(answer[0].numOfIENG);
$('#totalEmpText').val(answer[0].numOfEmployees);
$('#establishmentYearText').val(answer[0].yearOfEstablishment);
$("#fieldOfBusinessSelectBox option[value=answer[0].yearOfEstablishment]").attr('selected', 'selected');
},
error:function(){
alert("An error has occured !");
}
});
});
});
答案 0 :(得分:2)
要按照您尝试的方式执行此操作,只需采用语法修复:
$("#fieldOfBusinessSelectBox option[value=" + answer[0].yearOfEstablishment +"]").attr('selected', 'selected');
但是,你可以像对待所有其他人那样做:
$("#fieldOfBusinessSelectBox").val(answer[0].yearOfEstablishment);