最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .hide() doesn't work in some browsers - Stack Overflow

programmeradmin4浏览0评论

We are using jQuery .hide() to hide options in select inputs - when there are less than 31 days in a month. It works fine with Google Chrome and FireFox, but it doesn't work in Internet Explorer, Opera and Safari. Here is the JavaScript code we are using:

$(function() {
    // Show and hide days according to the selected year and month.
    function show_and_hide_days(fp_form) {
        var select_year= $(fp_form).find("select.value_year");
        var select_month= $(fp_form).find("select.value_month");
        var select_day= $(fp_form).find("select.value_day");
        var selected_year= parseInt($(select_year).val());
        var selected_month= parseInt($(select_month).val());
        var selected_day= parseInt($(select_day).val());
        var days_in_month= new Date(selected_year, selected_month, 0).getDate();
        if ((days_in_month >= 28))
        {
            // If selected day is bigger than the number of days in the selected month, reduce it to the maximal day in this month.
            if (selected_day > days_in_month)
            {
                $(select_day).val(days_in_month);
            }
            // Show all the days in this month and hide days which are not in this month.
            $(select_day).find("option").each(function() {
                var day= parseInt($(this).val());
                if (day <= days_in_month)
                {
                    $(this).show();
                }
                else
                {
                    $(this).hide();
                }
            });
        }
    }

    // Show and hide all days in this page.
    function show_and_hide_all_days() {
        $("select.value_day").each(function() {
            var form= $(this).closest("form");
            // Show and hide days according to the selected year and month.
            show_and_hide_days(form);
        });
    }

    // Show and hide all days in this page.
    show_and_hide_all_days();

    $("select.value_year, select.value_month").live("change", function() {
        var form= $(this).closest("form");
        // Show and hide days according to the selected year and month.
        show_and_hide_days(form);
        return false;
    });
});

Here is the HTML code:

<select class="value_year">
    <option value="2000">2000</option>
    <option value="2001">2001</option>
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2004</option>
    <option value="2005">2005</option>
    <option value="2006">2006</option>
    <option value="2007">2007</option>
    <option value="2008">2008</option>
    <option value="2009">2009</option>
    <option value="2010">2010</option>
    <option value="2011">2011</option>
    <option value="2012" selected="selected">2012</option>
    <option value="2013">2013</option>
</select>
/
<select class="value_month">
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12" selected="selected">12</option>
</select>
/
<select class="value_day">
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18" selected="selected">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
</select>

We are using jQuery v1.8.3 (I upgraded to this version to test if it fixes the problem, but it doesn't).

Thanks, Uri.

We are using jQuery .hide() to hide options in select inputs - when there are less than 31 days in a month. It works fine with Google Chrome and FireFox, but it doesn't work in Internet Explorer, Opera and Safari. Here is the JavaScript code we are using:

$(function() {
    // Show and hide days according to the selected year and month.
    function show_and_hide_days(fp_form) {
        var select_year= $(fp_form).find("select.value_year");
        var select_month= $(fp_form).find("select.value_month");
        var select_day= $(fp_form).find("select.value_day");
        var selected_year= parseInt($(select_year).val());
        var selected_month= parseInt($(select_month).val());
        var selected_day= parseInt($(select_day).val());
        var days_in_month= new Date(selected_year, selected_month, 0).getDate();
        if ((days_in_month >= 28))
        {
            // If selected day is bigger than the number of days in the selected month, reduce it to the maximal day in this month.
            if (selected_day > days_in_month)
            {
                $(select_day).val(days_in_month);
            }
            // Show all the days in this month and hide days which are not in this month.
            $(select_day).find("option").each(function() {
                var day= parseInt($(this).val());
                if (day <= days_in_month)
                {
                    $(this).show();
                }
                else
                {
                    $(this).hide();
                }
            });
        }
    }

    // Show and hide all days in this page.
    function show_and_hide_all_days() {
        $("select.value_day").each(function() {
            var form= $(this).closest("form");
            // Show and hide days according to the selected year and month.
            show_and_hide_days(form);
        });
    }

    // Show and hide all days in this page.
    show_and_hide_all_days();

    $("select.value_year, select.value_month").live("change", function() {
        var form= $(this).closest("form");
        // Show and hide days according to the selected year and month.
        show_and_hide_days(form);
        return false;
    });
});

Here is the HTML code:

<select class="value_year">
    <option value="2000">2000</option>
    <option value="2001">2001</option>
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2004</option>
    <option value="2005">2005</option>
    <option value="2006">2006</option>
    <option value="2007">2007</option>
    <option value="2008">2008</option>
    <option value="2009">2009</option>
    <option value="2010">2010</option>
    <option value="2011">2011</option>
    <option value="2012" selected="selected">2012</option>
    <option value="2013">2013</option>
</select>
/
<select class="value_month">
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12" selected="selected">12</option>
</select>
/
<select class="value_day">
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18" selected="selected">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
</select>

We are using jQuery v1.8.3 (I upgraded to this version to test if it fixes the problem, but it doesn't).

Thanks, Uri.

Share Improve this question asked Dec 18, 2012 at 10:11 UriUri 3,30112 gold badges50 silver badges88 bronze badges 5
  • 3 It isn't a problem with jQuery, but rather the browsers. <option> elements don't support many styling properties in a cross-browser way, that's case with the display property which jQuery .hide() sets. Your best bet is detaching said options instead of hiding them. – Fabrício Matté Commented Dec 18, 2012 at 10:15
  • 1 Also, always provide radix in parseInt because it behaves inconsistently among browsers without it. – skalee Commented Dec 18, 2012 at 10:18
  • Try .attr('disabled') == 'disabled'; and .removeAttr('disabled'); – Anujith Commented Dec 18, 2012 at 10:19
  • Why wouldn't you simply Disable/Enable those options upon change ? – Moseleyi Commented Dec 18, 2012 at 10:19
  • I have also some code style ment: instead of if (condition) { $(sth).show() } else { $(sth).hide() } write: $(sth).toggle(condition). Far more readable and shorter. jQuery docs – skalee Commented Dec 18, 2012 at 10:22
Add a ment  | 

2 Answers 2

Reset to default 10

it's a browser issue you can't hide options in some browser because $('.selector').hide(); is similar to $('.selector').css('display', 'none'); some browser can't hide it

you need to use $('.selector').remove(); and $('.selector').append();

change the codes from

 if ((days_in_month >= 28))
        {
            // If selected day is bigger than the number of days in the selected month, reduce it to the maximal day in this month.
            if (selected_day > days_in_month)
            {
                $(select_day).val(days_in_month);
            }
            // Show all the days in this month and hide days which are not in this month.
            $(select_day).find("option").each(function() {
                var day= parseInt($(this).val());
                if (day <= days_in_month)
                {
                    $(this).show();
                }
                else
                {
                    $(this).hide();
                }
            });
        }

to

// Remove days 29 - 31
$(select_day).find("option[value='29'], option[value='30'], option[value='31']").remove();
var daysOptions = "";

if (days_in_month >= 29) {
    daysOptions += '<option value="29">29</option>';
}
if (days_in_month >= 30) {
    daysOptions += '<option value="30">30</option>';
}
if (days_in_month == 31) {
    daysOptions += '<option value="31">31</option>';
}

$(select_day).append(daysOptions);

http://jsfiddle/sL4jY/10/ tested in IE chrome and Firefox

Thank you for your answer, I used your code but I changed it a little bit to handle months with 28 and 29 days (February). Here is the function again:

// Show and hide days according to the selected year and month.
function show_and_hide_days(fp_form) {
    var select_year= $(fp_form).find("select.value_year");
    var select_month= $(fp_form).find("select.value_month");
    var select_day= $(fp_form).find("select.value_day");
    var selected_year= $.parse_int($(select_year).val());
    var selected_month= $.parse_int($(select_month).val());
    var selected_day= $.parse_int($(select_day).val());
    var days_in_month= new Date(selected_year, selected_month, 0).getDate();
    // If the number of days in the selected month is less than 28, change it to 31.
    if (!(days_in_month >= 28))
    {
        days_in_month= 31;
    }
    // If the selected day is bigger than the number of days in the selected month, reduce it to the last day in this month.
    if (selected_day > days_in_month)
    {
        selected_day= days_in_month;
    }
    // Remove days 29 to 31, then append days 29 to days_in_month.
    for (var day= 31; day >= 29; day--)
    {
        $(select_day).find("option[value='" + day + "']").remove();
    }
    for (var day= 29; day <= days_in_month; day++)
    {
        $(select_day).append("<option value=\"" + day + "\">" + day + "</option>");
    }
    // Restore the selected day.
    $(select_day).val(selected_day);
}

It now works with all the five browsers I tested (I didn't test with previous versions of Internet Explorer).

I added a plugin to jQuery called $.parse_int - this returns parseInt with radix 10 if not specified. Here is the code of the plugin:

// Add functions to the jQuery object.
(function( $ ) {
    // Return parseInt with radix 10 if not specified.
    $.parse_int= function(fp_string, fp_radix) {
        var radix= ((typeof(fp_radix) !== "undefined") ? fp_radix : 10);
        return parseInt(fp_string, radix);
    };
})( jQuery );

Uri.

发布评论

评论列表(0)

  1. 暂无评论