简体   繁体   English

如何将下拉选项更改为使用jQuery选择的默认选项

[英]how to change a drop down options to default selected using jquery

Below code is part of my a html form drop down menu: 下面的代码是我的html表单下拉菜单的一部分:

<div class="wrap-quest-resp" id="fa8">
    <div class="input-quest-no-height">FA and port - 8</div>
    <div class="input-resp-no-height">
        <select id="fa-select-8" name="fa-select-8">
        <option disabled="disabled" SELECTED >Dir</option>
        <option <?php if ($_POST['fa-select-8']==1)  {echo "selected='selected'"; } ?> >1</option>
        <option <?php if ($_POST['fa-select-8']==2)  {echo "selected='selected'"; } ?> >2</option>
        <option <?php if ($_POST['fa-select-8']==3)  {echo "selected='selected'"; } ?> >3</option>
        <option <?php if ($_POST['fa-select-8']==4)  {echo "selected='selected'"; } ?> >4</option>
        <option <?php if ($_POST['fa-select-8']==5)  {echo "selected='selected'"; } ?> >5</option>
        </select>

        <select id="proc-select-8" name="proc-select-8">
        <option disabled="disabled" SELECTED >Proc</option>
        <option <?php if ($_POST['proc-select-8']==a) {echo "selected='selected'"; } ?> >a</option>
        <option <?php if ($_POST['proc-select-8']==b) {echo "selected='selected'"; } ?> >b</option>
        </select>

        <select id="port-select-8" name="port-select-8">
        <option disabled="disabled" SELECTED >Port</option>
        <option <?php if ($_POST['port-select-8']==0) {echo "selected='selected'"; } ?> >0</option>
        <option <?php if ($_POST['port-select-8']==1) {echo "selected='selected'"; } ?> >1</option>
        </select>
    </div>
    </div>

I a using this jquery to hide above mentioned div and its drop down selection 我使用这个jQuery来隐藏上面提到的div及其下拉选择

$('#fa8').slideUp("fast"); 

I would like to change the option selected to its default SELECTED option while hiding the div . 我想在隐藏div同时将option更改为其默认的SELECTED选项。 That is to below options for all three drop down menus 这是所有三个下拉菜单的以下选项

<option disabled="disabled" SELECTED >Dir</option>
<option disabled="disabled" SELECTED >Proc</option>
<option disabled="disabled" SELECTED >Port</option>

Use the callback from slideUp and set the 0 index of each to selected via prop() 使用来自slideUp的回调并将每个的0索引设置为通过prop()进行选择

$('#fa8').slideUp("fast", function() {
    //Set the 0 index as selected
    $("#fa-select-8 option").eq(0).prop("selected", true);
    //do the same for the rest, change the ID
}); 

Since you appear to want all of the selects to go to their default values, I'd suggest using this: 由于您似乎希望所有选择均使用默认值,因此建议使用以下方法:

$('#fa8').find("select").find("option:first").prop("selected", true);

Depending on how your code is implemented, you could make it part of a callback, as tymeJV suggested, or simply place it on the next line after the slideUp call in the code. 根据tymeJV的建议,取决于代码的实现方式,您可以使其成为回调的一部分,也可以将其放在代码中slideUp调用之后的下一行。

If you use the callback, you can use $(this) instead of $('#fa8') : 如果使用回调,则可以使用$(this)代替$('#fa8')

$('#fa8').slideUp("fast", function() {
    $(this).find("select").find("option:first").prop("selected", true);
});

If you go for the "multi-line" approach, store off the selector for $('#fa8') in a variable, so that you are not running the select twice: 如果您采用“多行”方法,请将$('#fa8')的选择器存储在变量中,以免再次运行select:

var $wrapReqResp = $('#fa8');

$wrapReqResp.slideUp("fast");
$wrapReqResp.find("select").find("option:first").prop("selected", true);

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

相关问题 如何使用jQuery在下拉列表中的“选定”属性中获得#选项? - How to get the # of options with the attr “selected” in a drop down using jQuery? 使用JavaScript或jQuery或CSS在下拉菜单中更改选项的颜色 - Change the color of the options a drop down menu using JavaScript or jQuery or CSS 使用JQuery更改下拉菜单的选定选项 - Using JQuery to change a drop-down menu's selected option 使用jQuery和javascript在下拉列表中更改选定的值 - Change the selected value in a drop down using jQuery and javascript 如何使用jQuery / JS设置下拉菜单项? - How to set drop down item as selected using jQuery/JS? 如果选择了两个选项之一,则Greasemonkey脚本可更改下拉选项 - Greasemonkey Script to change drop down option if either of 2 options are selected 如何使用asp.net中的另一个下拉列表更改下拉列表选择的索引和可见性? - How to change drop down list selected index and visibility using another drop down list in asp.net? 根据另一个选定的下拉列表更改下拉列表中的选项 - Change options in a drop-down list depending on another selected drop-down list 下拉值应更改为选定的选项,而不是默认的角度 - drop down value should change to selected option and not default- angular 如何根据下拉菜单中选择的选项隐藏/显示文本字段? - How to hide/show text fields based on options selected in a drop down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM