我有两个这样的下拉列表:
<select onload="javascript:loadState(this.value)" onchange="javascript:loadState(this.value)" id="cntry" name="country">
<option label="United States" value="1">United States</option>
<option label="Afghanistan" value="2">Afghanistan</option>
</select>
<select id="state" name="state">
<option label="Alabama" value="1">Alabama</option>
<option label="Alaska" value="2">Alaska</option>
<option label="Arizona" value="3">Arizona</option>
<option label="Arkansas" value="4">Arkansas</option>
<option label="California" value="5">California</option>
</select>
function loadState(countryId)
{
$j.ajax({
type: "GET",
url: baseUrl+'/profile/load-states/country-id/'+countryId,
data: "",
dataType: "xml",
error: function (XMLHttpRequest, textStatus, errorThrown) {
$j("#message").html("<p>There is problem with the ajax request.</p>");
return false;
},
success: function(xml){
var htmlOptions = '';
$j(xml).find("State").each(function() {
var states = $j(this);
var stateId = states.attr("Id");
var stateName = states.attr("Name");
htmlOptions += "<option value='"+ stateId +"'>"+stateName+"</option>";
});
if(htmlOptions){
$j("#state").html(htmlOptions);
}
else
{
htmlOptions = "<option value='0'>Select State</option>";
$j("#state").html(htmlOptions);
}
}
});
}
在这里,当我们选择一个国家/地区时,它会自动填充该国家/地区的状态,作为 loadState 功能完美运行的第二个下拉列表。问题是当发生验证错误时,第二个下拉菜单有时会保留其默认值,这些状态与所选国家/地区无关。解决方案是在第一个下拉菜单的 onload 中触发该功能,但它不起作用。