[英]Reset selected attribut of select option
我知道這是一個主流問題,但我已經搜索了很多,但沒有找到任何答案。 我只需要重置我的字段。
所以我有一個 php 類 (classMenu.php),它為我的主頁生成一個 html 表單。 為了獲得最后選擇的選項,當我像這樣調用我的 classMenu.php 時,我將 $_POST 作為參數:
主頁.php
new menu("mainPage.php", $_POST);
類菜單.php
<select class="selectpicker" id="eventType" title="Event Type" name="eventType" data-width="150px" data-live-search="true" data-actions-box="true">
<option value="workshop"
<?php
$this->isSelected("eventType", "workshop")
?>
>Workshop</option>
<option value="master" <?php $this->isSelected("eventType", "master") ?>>Master</option>
<option value="webinar" <?php $this->isSelected("eventType", "webinar") ?>>Webinar</option>
</select>
這是我的 isSelected 函數($setValues = $_POST 參數):
private function isSelected($field, $value){
if(isset($this->setValues[$field]) && $this->setValues[$field] == $value){
echo "selected";
}
}
我已經為我的 javascript 代碼嘗試了這些選項:
function resetSelect() {
$('#eventType').prop('selectedIndex', 0);
};
function resetSelect() {
document.getElementById('eventType')[0].selectedIndex = 0;
};
function resetSelect() {
$("#eventType").val('');
};
function resetSelect() {
$('select').each(function(){
$(this).find('option:selected').prop('selected', false);
});
};
function resetSelect() {
$('select').each(function(){
$(this).find('option:selected').removeAttr('selected');
});
};
我希望我的問題對你來說很清楚。
謝謝
jQuery:將selectedIndex
設置為 0:
$('#eventType').prop('selectedIndex', 0);
如果使用javascript:
document.getElementsById('eventType')[0].selectedIndex = 0
如我所見,我會使用:
$('option:selected').removeAttr('selected');
如果您有多個選擇,那么:
$('.selectpicker').each(function(){
$(this).find('option:selected').removeAttr('selected');
});
$('.selectpicker').prop('selectedIndex', 0);
$('.selectpicker').selectpicker('refresh');
你也可以使用
$('option:selected').prop('selected', false)
這段代碼對我有用。 $('.multiselect-ui option').prop('selected', false).trigger('chosen:updated');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.