[英]How to set a select element value to default after being manipulated by jquery ?
I have a form for my user to search for something based on the category and location, and the location is based on state and city options. 我有一个表单供用户根据类别和位置搜索内容,并且位置基于州和城市选项。 So for each state option, there is a city select element consisting of list of cities in the state.
因此,对于每个州选项,都有一个由城市中的城市列表组成的城市选择元素。 (ie There are 10 states option in the states select, each state option will show a different list of cities select element. so if state1 option is selected, list of cities in state1 will be shown and others will be hidden, if state2 is selected, list of cities in state2 will be shown and others will be hidden and so on).
(即,在选择的状态中有10个状态选项,每个状态选项将显示不同的城市选择元素列表。因此,如果选择了state1选项,则将显示state1中的城市列表,如果选择了state2,则其他城市将被隐藏,将显示state2中的城市列表,其他城市将被隐藏,依此类推)。 I am using two jquery scripts for these select elements.
我正在为这些选择元素使用两个jquery脚本。
The first script is to manipulate the value of state and city element after a user search, so that if they are going to change category, they would not need to re-select the state and city select elements as they are already set to the value that the user searched. 第一个脚本是在用户搜索后操纵state和city元素的值,这样,如果要更改类别,他们将不需要重新选择state和city选择元素,因为它们已经设置为value用户搜索的内容。
The second script is to show/hide the cities select element based on the states option selected. 第二个脚本是根据选定的州选项显示/隐藏城市选择元素。
The problem is, after a user search for a example, category1 option, state2 option, and city1 option of state2, then if the user want to change to state3 option, the state3 cities list select will be blank, because the value is set to the state2 city value, how do I remove this value, so that when a user change to another state, the city select element will have default value(no value set) ? 问题是,在用户搜索了state2的category1选项,state2选项和city1选项之后,如果用户想要更改为state3选项,则state3城市列表选择将为空,因为该值设置为state2城市值,如何删除此值,以便当用户更改为另一个州时,城市选择元素将具有默认值(未设置值)?
I am sorry if my explanation a bit unclear. 如果我的解释不清楚,我很抱歉。 Anyway here is the code for clearer picture.
无论如何,这里是更清晰图片的代码。 Thx.
谢谢。
html : html:
<select name='category'>
<option value='category1'>Category1</option>
<option value='category2'>Category2</option>
..
</select>
<select name='state' class='state'>
<option value='state1' class='cities_state1'>State1</option>
<option value='state2' class='cities_state2>State2</option>
...
</select>
<select name='city' class='cities' id='cities_state1' disabled>
<option value='city_1_in_state1'>City 1 in state1</option>
<option value='city_2_in_state1'>City 2 in state1</option>
...
</select>
<select name='city' class='cities' id='cities_state2' disabled>
<option value='city_1_in_state2'>City 1 in state2</option>
<option value='city_2_in_state2'>City 2 in state2</option>
...
</select>
css : css:
.cities {
display: none;
}
jquery script 1 (set value of city so that user does not need to re-select) : jQuery脚本1(城市的设置值,以便用户无需重新选择):
$(document).ready(function() {
$('.state').val('<?php echo $_GET['state'];?>');
//get the class attribute of option selected in state select element
var city = $('.state option:selected').attr('class');
//Show cities list of selected state
$('#' + city).show();
$('#' + city).prop('disabled', false);
//Hide other cities list not of selected state
$(.cities).not('#' + city).hide();
$(.cities).not('#' + city).prop('disabled', true);
//manipulate value of city select
$(.cities).val('<?php echo $_GET['city'];?>');
});
Jquery script 2 (hide/show everytime state option changed) : jQuery脚本2(更改/隐藏每次显示状态选项):
$(document).ready(function() {
$('.state').change(function() {
//get the class attribute of option selected in state select element
var city = $('.state option:selected').attr('class');
//Show cities list of selected state
$('#' + city).show();
$('#' + city).prop('disabled', false);
//Hide other cities list not of selected state
$(.cities).not('#' + city).hide();
$(.cities).not('#' + city).prop('disabled', true);
});
This is how the process that I want : 这就是我想要的过程:
I have tried using removeattr('selected'), prop('selected', false), but still unable to make the city select to show the first option when state is changed. 我尝试使用removeattr('selected'),prop('selected',false),但在状态更改时仍然无法使城市选择显示第一个选项。 Sorry for the quite messy explanation and code.
对不起,我的解释和代码都很混乱。 All suggestion to achieve what I need is very welcomed.
所有实现我需要的建议都受到欢迎。 Thx in advance
提前Thx
Try below code - 尝试下面的代码-
NOTE - you need not to use document.ready
multiple times. 注意 -您无需多次使用
document.ready
。 Use it once and put all your script code inside it. 只需使用一次,然后将所有脚本代码放入其中。
$(document).ready(function() {
//set initial code when page loads
//set state1 selected
$('.state').val('state1');
//get class value for selected state and use it to select city
var city = $('.state option:selected').attr('class');
//Hide other cities list not of selected state
$('.cities').hide();
//Show cities list of selected state
$('#' + city).show();
//manipulate value of city select
$('.cities').val('city_1_in_state1');
//Add state select box listener
$('.state').change(function() {
//get the class attribute of option selected in state select element
var city = $(this).find('option:selected').attr('class');
//Hide other cities list not of selected state
$('.cities').hide();
//Show cities list of selected state
$('#' + city).show();
//set non blank option to select
$('#' +city).find('option:not(:empty):first').prop('selected',true);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.