简体   繁体   English

如何在由jquery操纵后将选择元素值设置为默认值?

[英]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 : 这就是我想要的过程:

  1. User select a category, state and a city and search 用户选择类别,州和城市并搜索
  2. Page return with result, and state and city select has the value of the search (incase user want to select another category, user does not need to re-select state and city). 返回带有结果的页面,并且州和城市选择具有搜索的值(如果用户要选择其他类别,则用户无需重新选择州和城市)。
  3. But if user want to choose another city of another state, when user change another state, the city select should show the value of the first option in that select element, because now if a user change a state, it will show the city list select of that element but with blank, instead of the first option of the select. 但是,如果用户想选择另一个州的另一个城市,则当用户更改另一个州时,城市选择应该在该选择元素中显示第一个选项的值,因为现在如果用户更改了一个州,它将显示选择的城市列表元素,但为空白,而不是select的第一个选项。

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);
   });
});

JSFiddle Demo JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM