【问题标题】:Set value in Jquery, added a two times the same value在 Jquery 中设置值,添加两次相同的值
【发布时间】:2016-08-02 17:06:17
【问题描述】:

这是实际内容。在下面的代码中,在 UI 中的下拉列表中添加了两次值。将下拉值显示为“AprilApril”而不是“April”

 if ($('#dk_container_selectToYear .dk_options li.dk_option_current a').text() == 2016) {
            var currentdate = new Date();
            var month = currentdate.getMonth();
            var monthname = monthLookup[currentdate.getMonth()];
            month = month + 3;
            while (month <= 13) {
                $('#dk_container_selectToMonth .dk_options li:nth-child(' + month + ')').css({ "display": 'none' });
                var listvalue = $('#dk_container_selectToMonth .dk_options li:nth-child(' + month + ')').text();

                if ($('#dk_container_selectToMonth .dk_options li.dk_option_current a').text() == listvalue) {
                    $('#selectToMonth').dropkick('reset');
                    $('#selectToMonth').dropkick('setValue', monthname);
                }

                month++;
            }
        }


$('#selectToMonth').dropkick('setValue', monthname);

是否有其他选择可以避免它..

提前谢谢..

【问题讨论】:

  • monthLookup 是什么样的?
  • 如果你在设置后控制台日志monthname,你会看到什么?

标签: javascript jquery duplicates setvalue


【解决方案1】:

!!这不是您问题的解决方案,但在您找到解决方案之前它只是一个技巧。 只需抓住字符串的一半..就像这样:

var monthname = 'AprilApril';
var month = monthname;
console.log(month.substr(0, (month.length/2))); // April
console.log(month) // AprilApril

享受编码!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    相关资源
    最近更新 更多