Javascript 如何使用jquery将下拉选项更改为默认选中
以下代码是我的html表单下拉菜单的一部分:Javascript 如何使用jquery将下拉选项更改为默认选中,javascript,jquery,html,Javascript,Jquery,Html,以下代码是我的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">
<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>
我想在隐藏div
的同时,将所选选项更改为默认的selected
选项。这是所有三个下拉菜单的以下选项
<option disabled="disabled" SELECTED >Dir</option>
<option disabled="disabled" SELECTED >Proc</option>
<option disabled="disabled" SELECTED >Port</option>
Dir
过程
港口
使用来自slideUp的回调
并通过prop()
由于您似乎希望所有选择都返回其默认值,因此我建议使用以下方法:
$('#fa8').find("select").find("option:first").prop("selected", true);
根据代码的实现方式,您可以按照tymeJV的建议将其作为回调的一部分,或者简单地将其放在代码中slideUp
调用后的下一行
如果使用回调,则可以使用$(this)
而不是$('fa8')
:
如果使用“多行”方法,请将$('#fa8')
的选择器存储在一个变量中,这样就不会运行select两次:
var $wrapReqResp = $('#fa8');
$wrapReqResp.slideUp("fast");
$wrapReqResp.find("select").find("option:first").prop("selected", true);
$('#fa8').find("select").find("option:first").prop("selected", true);
$('#fa8').slideUp("fast", function() {
$(this).find("select").find("option:first").prop("selected", true);
});
var $wrapReqResp = $('#fa8');
$wrapReqResp.slideUp("fast");
$wrapReqResp.find("select").find("option:first").prop("selected", true);