【问题标题】:how to change a drop down options to default selected using jquery如何使用jquery将下拉选项更改为默认选择
【发布时间】:2013-10-14 09:11:41
【问题描述】:

以下代码是我的 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>

我使用这个 jquery 来隐藏上面提到的 div 及其下拉选择

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

我想将选中的 option 更改为其默认的 SELECTED 选项,同时隐藏 div 。这就是所有三个下拉菜单的选项

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

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    使用来自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
    }); 
    

    【讨论】:

      【解决方案2】:

      由于您似乎希望所有选择都恢复为默认值,因此我建议您使用:

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

      根据您的代码实现方式,您可以按照 tymeJV 的建议将其作为回调的一部分,或者简单地将其放在 @987654322 之后的下一行@在代码中调用。

      如果使用回调,可以使用$(this)代替$('#fa8'):

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

      如果您选择“多行”方法,请将 $('#fa8') 的选择器存储在一个变量中,这样您就不会运行两次选择:

      var $wrapReqResp = $('#fa8');
      
      $wrapReqResp.slideUp("fast");
      $wrapReqResp.find("select").find("option:first").prop("selected", true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-13
        • 1970-01-01
        • 2012-12-24
        • 1970-01-01
        • 1970-01-01
        • 2011-06-19
        相关资源
        最近更新 更多