Jquery 添加一个选项来选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15595463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:23:51  来源:igfitidea点击:

Jquery adding an option to select

jqueryselectappendoption

提问by LPB

I have tried solutions on other questions answered on stackoverflow, but none of them worked for me. In a form, I want to update the number of days in a month depending on the month that the user chooses.

我已经尝试解决在 stackoverflow 上回答的其他问题,但没有一个对我有用。在表单中,我想根据用户选择的月份更新一个月中的天数。

#DOBMis the ID of the month select list (DOBM stands for Date Of Birth Month)
#DOBDis the ID of the day select list (DOBD stands for Date Of Birth Day)
Basically, if #DOBMhas values of 01,03,05,07,08,10,12, we are in a month with 31 days.

#DOBM是月份选择列表的 ID(DOBM 代表出生月份)
#DOBD是日期选择列表的 ID(DOBD 代表出生日期)
基本上,如果#DOBM有 01,03,05,07,08 的值, 10、12,我们一个月有31天。

Here is my code:

这是我的代码:

if ((($('#DOBM').val() == '01'||'03'||'05'||'07'||'08'||'10'||'12'))
&& ($("#DOBD option[value='31']").length == 0)) {
    $("#DOBD").append("<option value='31'>31</option>");}

The second line of code is to see if the option already exists in the day select list. The last line of code is to append the new option (day 31) if we are in a month of 31 days AND if option 31 doesn't already exist.

第二行代码是查看日选择列表中是否已经存在该选项。如果我们处于 31 天的月份并且如果选项 31 尚不存在,则最后一行代码是附加新选项(第 31 天)。

The code just doesn't update the DOBD select list. Any idea on what I am doing wrong?

该代码只是不更新​​ DOBD 选择列表。知道我做错了什么吗?

Thanks.

谢谢。

Edit: i'm new on stackoverflow; if only I could up your answers, they both helped me a lot.

编辑:我是 stackoverflow 的新手;如果我能回答你的问题,他们都帮了我很多。

回答by Hanlet Esca?o

Your if statement is wrong. You have to compare the .val()with each value as such:

你的 if 语句是错误的。您必须将.val()与每个值进行比较:

if($('#DOBM').val() == '01'||$('#DOBM').val() == '03'

EDIT

编辑

Optionally you could also use the switch statement:

您也可以选择使用 switch 语句:

$("#btn").on("click",function(){
    switch($("#DOBM").val())
    {
        case "01":
        case "03":
        case "05":
        case "07":
        case "08":
        $("#DOBD").append("<option value='31'>31</option>");
            break;
    }
});

JSFiddle: http://jsfiddle.net/dRJHh/

JSFiddlehttp: //jsfiddle.net/dRJHh/

With array, as per @sberry suggestion:

使用数组,根据@sberry 建议:

var vals = ['01','03', '05', '07', '08'];
$("#btn").on("click",function(){

    if(jQuery.inArray($("#DOBM").val(),vals) > -1)
    {
         $("#DOBD").append("<option value='31'>31</option>");
    }
});

jsFiddle: http://jsfiddle.net/yhcDa/

jsFiddlehttp: //jsfiddle.net/yhcDa/

回答by Subodh Ghulaxe

var month = $('#DOBM').val();    
if ((( month == '01')||(month =='03')||(month =='05')||(month =='07')||(month =='08')||(month =='10')||(month =='12')) && ($("#DOBD option[value='31']").length == 0)) 
 {
        $("#DOBD").append("<option value='31'>31</option>");
 }

Or you can also do this

或者你也可以这样做

var month = $('#DOBM').val();
var arr = [ '01','03', '05','07', '08', '10', '12'];
if(($.inArray(month, arr) != -1) && ($("#DOBD option[value='31']").length == 0))
{
     $("#DOBD").append("<option value='31'>31</option>");
}

Or you can directly check number of days in month

或者您可以直接查看月份的天数

var month = $('#DOBM').val();
var year = 2013;
if((Date(year, month, 0).getDate() == 31) && ($("#DOBD option[value='31']").length == 0)) {
    $("#DOBD").append("<option value='31'>31</option>");
}