2017-02-10 145 views
2

我知道这是一个主流问题,但我已经搜索了很多,我没有找到任何答案。 我只需要重置我的领域。重置选择attribut的选择选项

所以我有一个PHP类(classMenu.php)谁为我的主页生成html表单。 要获得最后一次选择的选项,我过去$ _ POST作为参数,当我把我的classMenu.php这样的:

mainPage.php

new menu("mainPage.php", $_POST); 

classMenu.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 par ameter):

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

我希望我的问题是清楚的为您服务。

感谢

+0

你试过'$( “#EVENTTYPE”)VAL( '') ' – Satpal

+0

@Satpal是的,我已经试过了,但没有发生任何事情:/ 无论如何感谢, – scabiati

回答

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)

+0

我试过你的代码,但暂时没有改变:/ 感谢您的帮助 – scabiati

1

jQuery的:道具selectedIndex以0:

$('#eventType').prop('selectedIndex', 0);

如果用javascript:

document.getElementsById('eventType')[0].selectedIndex = 0

+0

我已经尝试过,但它没有改变任何东西。 谢谢 – scabiati

+0

浏览器开发者控制台中有任何错误消息? –

+0

我的控制台中没有任何jQuery代码错误。 我在我的功能结束时调用一个提醒,并且她总是执行。 – scabiati