对于尴尬的帖子标题感到抱歉 - 我不确定如何说出来。
我有一个表单,要求用户选择他最喜欢的商店位置。有3个选择框,其中包含城市,州和街道地址值。我能够使用我在另一篇文章中找到的代码来设置表单,因此当用户选择状态时,仅显示该州的城市,并且一旦选择了城市,则仅显示该城市中的商店。那部分工作得很好。
但是,我还需要根据所选商店再传递2个值,商店编号和地区ID。我已经花了几个小时来编写脚本,并且无法弄清楚如何分配和传递其他值。我是jQuery的新手,仍然无法完全识别每个函数正在做什么。任何帮助将不胜感激!
以下是我的代码示例:
<script type="text/javascript">
$(document).ready(function () {
$('select#FavState').change(function (e) {
var cityOptions = getCityOptions($(this).val());
$('select#FavCity').html(''); // clear the existing options
$.each(cityOptions, function (i, o) {
$('<option>' + o + '</option>').appendTo('select#FavCity');
});
});
$('select#FavCity').change(function (e) {
var streetOptions = getStreetOptions($(this).val());
$('select#FavStreet').html(''); // clear the existing options
$.each(streetOptions, function (i, o) {
$('<option>' + o + '</option>').appendTo('select#FavStreet');
});
});
function getCityOptions(val) {
if (val == '0') return ['Select City'];
if (val == 'CT') return ['Avon', 'Bridgeport', 'Bristol', 'Cromwell', 'Danbury', 'Darien', 'East Windsor', 'Enfield', 'Hartford', 'Manchester', 'Milford', 'Mystic', 'Naugatuck', 'Newington', 'North Haven', 'Norwich', 'Plainville', 'Southbury', 'Southington', 'Unionville', 'Vernon', 'Waterbury', 'Wethersfield', 'Willimantic', 'Windsor Locks'];
if (val == 'DE') return ['Dover', 'Hockessin', 'Middletown', 'Newark', 'Rehoboth Beach', 'Seaford', 'Wilmington'];
}
function getStreetOptions(val) {
if (val == '0') return ['Select Street'];
if (val == 'Avon') return ['347 West Main Street'];
if (val == 'Bridgeport') return ['4545 North Main Street'];
if (val == 'Bristol') return ['240 Buckland Street'];
if (val == 'Cromwell') return ['48 Berlin Road'];
}
});
</script>
表单上的选择字段:
<form>
<select name="FavState" id="FavState">
<option selected="selected" value="0">Select State</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
</select>
<select name="FavStreet" id="FavStreet" class="formFields longSelectLists">
<option selected="selected" value="0">Select Street</option>
</select>
<input name="StoreNumber" id="StoreNumber" type="hidden" />
<input name="RegionID" id="RegionID" type="hidden" />
</form>
我尝试将此添加到getStreetOptions函数:
if (val == 'Avon') return ['347 West Main Street'];
$('input#StoreNumber').val('4239');
$('input#RegionID]').val('6');
但是我想我需要在其中添加一个函数,所以有某种触发器?
答案 0 :(得分:0)
以对象的形式存储
var address = {};
if( val == 'Avon') {
address["street"] = ['347 West Main Street'];
address["StoreNumber"] = 4239;
address["region"] = 6;
}
return address;
然后使用地址对象并将其显示在相应的字段中。
你可以尝试这段代码..虽然可以重新考虑
// Store the info in the form of nested objects so that it is easier to retrieve and change
var city = {
'0': ['Not Available'],
'CT': ['Avon', 'Bridgeport', 'Bristol', 'Cromwell', 'Danbury', 'Darien', 'East Windsor', 'Enfield', 'Hartford', 'Manchester', 'Milford', 'Mystic', 'Naugatuck', 'Newington', 'North Haven', 'Norwich', 'Plainville', 'Southbury', 'Southington', 'Unionville', 'Vernon', 'Waterbury', 'Wethersfield', 'Willimantic', 'Windsor Locks'],
'DE': ['Dover', 'Hockessin', 'Middletown', 'Newark', 'Rehoboth Beach', 'Seaford', 'Wilmington']
}, street = {
'0': {
'address': ['Not Available'],
'StoreNumber': 0,
'RegionID': 0
},
'Avon': {
'address': ['347 West Main Street'],
'StoreNumber': 4239,
'RegionID': 7
},
'Bridgeport': {
'address': ['4545 North Main Street'],
'StoreNumber': 5656,
'RegionID': 12
},
'Bristol': {
'address': ['240 Buckland Street'],
'StoreNumber': 8976,
'RegionID': 123
},
'Cromwell': {
'address': ['48 Berlin Road'],
'StoreNumber': 8976,
'RegionID': 123
}
};
// Cache your selectors so that you walk the DOM only once
var $favState = $('#FavState'),
$favCity = $('#FavCity'),
$favStreet = $('#FavStreet'),
$storeInput = $('#StoreNumber'),
$regionInput = $('#RegionID');
$(document).ready(function () {
$favState.change(function (e) {
var value = this.value;
// Access the object like city['CT'] .. That gives the Array
city[value] !== undefined ? city[value] : '0';
var cityOptions = city[value];
$favCity.html(''); // clear the existing options
$.each(cityOptions, function (i, o) {
$('<option>' + o + '</option>').appendTo('#FavCity');
});
});
$favCity.change(function (e) {
var value = this.value;
// Access the object like street['Avon'] .. That gives an object
value = street[value] !== undefined ? value : '0';
// This returns array
var streetOptions = street[value].address,
storeNumber = street[value].StoreNumber,
regionID =street[value].RegionID;
$favStreet.html(''); // clear the existing options
$.each(streetOptions, function (i, o) {
$('<option>' + o + '</option>').appendTo('#FavStreet');
});
// Storing the values in Hidden fields
$storeInput.val(storeNumber);
$regionInput.val(regionID);
console.log('Address - ' + $favStreet.val() +
' :: Store -' + $storeInput.val() +
' :: Region - ' + $regionInput.val() );
});
});
<强> Check Fiddle 强>