javascript - 下拉值在满足条件时重置为第一个值

标签 javascript jquery css drop-down-menu

在下面的代码中,如果当前下拉值指向 listvalues 变量中可用的值,则下拉选择的值应更改为第一个值。

   if (selectedForYear() == 2016) {
         var d = new Date();
         var month = d.getMonth();
         month = month + 3;
         while (month <= 13) {
             $('#dk_container_selectForMonth .dk_options li:nth-child(' + month + ')').css({ "display": 'none' });
             var listvalues = $('#dk_container_selectForMonth .dk_options li:nth-child(' + month + ')').text();
             if ($('#dk_container_selectForMonth .dk_options li.dk_option_current a').text() == listvalues) 
            {

                 $("#dk_container_selectForMonth").val($("#dk_container_selectForMonth li:first").val());

              }
             month++;
         }
     }

我试过这个:

$("#dk_container_selectForMonth").val($("#dk_container_selectForMonth li:first").val());

但它不起作用。

这是 HTML:

<div class="flexContainer mediumGutter">

    <div class="flexContent">
        <select data-bind="options: month, optionsCaption: 'Month', value: selectedForMonth" id="selectForMonth" tabindex="8" style="width: 130px">
            <option>month</option>
        </select>
    </div>
    <div class="flexContent">
        <select data-bind="options: year, optionsCaption: 'Year', value: selectedForYear" id="selectForYear" tabindex="9" style="width: 130px">
            <option>year</option>
        </select>
    </div>

有什么办法吗?

最佳答案

很抱歉回答我的问题。答案可能对某人有帮助。

通过使用 jquery dropkick() 方法刷新下拉菜单,下拉菜单得到刷新并分配第一个值。

$('#selectForMonth').dropkick('refresh');

关于javascript - 下拉值在满足条件时重置为第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301921/

相关文章:

javascript - VueJs 2、NodeJs 和重新加载页面

jquery - Bootstrap 3.1.1 缩略图 slider

javascript - 如何使用 Bootstrap 验证?

css - Content Div 内的 100% Height Div

html - 更改使用 wordpress 插件填充的内容选项卡的颜色?

javascript - 免除表单的可点击行为

javascript - 我如何从这个函数构造函数中获取全名

javascript - React Redux - 在没有连接的情况下调用分派(dispatch)(不是来自 react 组件)

javascript - 如何通过 PHP 更改 HTML 类?

css - 更改 Jekyll Minima(默认)主题中的字体