简体   繁体   English

如何在Firefox以外的所有其他浏览器中实现此功能

[英]How to make this work in all other browsers other than Firefox

I have a select list with numbers in it. 我有一个选择列表,里面有数字。 These numbers go from 0 to 30. I want to hide numbers based on the number of days apart from the current date and the date the user set. 这些数字从0到30。我想根据除当前日期和用户设置的日期以外的天数来隐藏数字。

So if today is July 28th 2010 and they Set July 29th 2010 it should only show "0". 因此,如果今天是2010年7月28日,并且它们设置为2010年7月29日,则应该仅显示“ 0”。

If it was July 28th 2010 and they set September 20th 2010 it should show 0 to 30. 如果是2010年7月28日,而他们将时间设置为2010年9月20日,则应显示0到30。

So I have this 所以我有这个

  var selectedDate = new Date($('#TextBox').val().replace(/\/(\d\d)$/, "/20$1"));
    var currentDate = new Date();

    var month = currentDate.getMonth() + 1
    var day = currentDate.getDate()
    var year = currentDate.getFullYear()

    currentDate = new Date(month + "/" + day + "/" + year);


    if (isNaN(selectedDate) == false)
    {
        $('#selectList').find('select').attr('disabled', '');

        var diffDays = parseInt((selectedDate  - currentDate) / (1000 * 60 * 60 * 24));

        var Options = $('#selectList').find('option');

        jQuery.each(Options, function (i, value)
        {
            var currentValue = $(this).val();
            if (currentValue == -1)
            {
                // equal to continue;
                return true;
            }
            else if (currentValue >= diffDays)
            {
                $(this).hide();
            }
            else
            {
                $(this).show();
            }
        });
    }

This code happens on change of the textbox of where the user would select a date. 此代码在用户选择日期的文本框更改时发生。

This works fine in FireFox but does not work in any other browser. 这在FireFox中工作正常,但在任何其他浏览器中均无效。 I don't know why. 我不知道为什么 No errors are shown in any of the browsers. 在任何浏览器中均未显示任何错误。

You simply can't hide/show <option> elements like this cross-browser, you need to either have a backup/hidden <select> and copy only the <option> elements you want each time, or just disable the <option> elements you don't want to be selectable, this will however leave them visible. 您根本无法像这样的跨浏览器那样隐藏/显示<option>元素,您需要具有备份/隐藏的<select>并每次仅复制您想要的<option>元素,或者只是禁用<option>不想被选择的元素,但是这将使它们可见。

The cloning bit would look something like this: 克隆位看起来像这样:

var hiddenSelect = $("#selectList").find('select').clone();
var selectedDate = new Date($('#TextBox').val().replace(/\/(\d\d)$/, "/20$1"));
var currentDate = new Date();

var month = currentDate.getMonth() + 1
var day = currentDate.getDate()
var year = currentDate.getFullYear()

currentDate = new Date(month + "/" + day + "/" + year);


if (isNaN(selectedDate) == false) {
    $('#selectList').find('select').attr('disabled', '');
    var diffDays = parseInt((selectedDate  - currentDate) / (1000 * 60 * 60 * 24));

    var select = $('#selectList').find('select').empty();
    hiddenSelect.children().each(function(i, value) {
        if (value == -1) {
            return true;
        }
        else if (currentValue < diffDays) {
            $(this).clone().appendTo(select);
        }      
    });
}

We're just keeping a cloned copy of the original in a variable called hiddenSelect , emptying (via .empty() ) out the options on the visible one in the UI, and looping through/cloning the options you want to see back into that visible select. 我们只是将原始副本的克隆副本保留在名为hiddenSelect的变量中,(通过.empty() )清空UI中可见选项的选项,然后遍历/克隆查看的选项可见选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript如何在FireFox中完美地工作,而在其他浏览器中却根本不工作? - How can javascript work perfectly in FireFox, but not work at all in other browsers? waveurfer.js-使它在Firefox以外的浏览器中运行 - wavesurfer.js - getting it to work in browsers other than Firefox JPlayer在Firefox Android上不起作用,但在所有其他浏览器上 - JPlayer Does Not Work On Firefox Android But All Other Browsers 简单的Javascript在FireFox中不起作用(但在所有其他浏览器中都有效) - Simple Javascript doesn't work in FireFox (but does in ALL other Browsers) scrollTop 在 Firefox 以外的浏览器中不起作用 - scrollTop not working in browsers other than Firefox 如何使该texarea javascript在Internet Explorer以外的其他浏览器中工作 - how to make this texarea javascript work in other browsers except Internet Explorer 如何使document.body.onmousedown = ContextMouseDown; 在其他浏览器中工作? - How to make document.body.onmousedown = ContextMouseDown; work in the other browsers? Firefox无法识别Java代码,但可以在所有其他浏览器中使用 - Javascript code not understood by Firefox But works in all other browsers Javascript firefox无法找到所有其他浏览器所做的值 - Javascript firefox can't find values all other browsers do $ .post不适用于最新的Firefox,但可用于所有其他浏览器 - $.post not working in latest firefox but works in all other browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM