简体   繁体   English

使用javascript或jquery显示下拉值

[英]display drop down values using javascript or jquery

I have following code to add values in drop down, values are getting added but that are not getting displayed in the drop down, i have tested it by reading values after adding in drop down by using jquery fruntions its get red but why should that not displayed in dropdown can you please tell me a solution about that. 我有以下代码在下拉列表中添加值,正在添加值,但未在下拉列表中显示,我通过使用jquery褶皱添加下拉列表后读取值来测试它的测试结果,但是为什么不这样做显示在下拉菜单中,您能否告诉我有关此问题的解决方案。

1) drop down 1)下拉

  <select id="year" name="year" onchange="test2(this.value)">
    <option value="">Choose Year</option>

2) javascript and jquery code 2)javascript和jquery代码

function getVa2()
    {


     var jSelect=$("select[name='year']");
            jSelect.removeAttr('disabled');

    var lnk =new Array();
    var cnt=0;
    var val=new Array

    $("#data1 a").each(function(index){
    if($(this).attr("class")=="subcategory_link")
    {



    lnk[cnt]=$(this).attr("href");


     cnt=cnt+1;

    }
    });
    cnt=0;
    $("#data1 span").each(function(ind){
    if($(this).attr("class")=="subcategory_name")
    {
      val[cnt]=$(this).text();

      cnt=cnt+1;

    }
    });



    var select1 = document.getElementById("year");

    $("#year").empty();

    select1.options[select1.options.length]=new Option("[ Select Your Series ]","");

        for(i=0; i<lnk.length;i++)
        {

        select1.options[select1.options.length] = new Option(val[i], lnk[i]);
        }

              //This displayes values from drop down in alert

    $("#year option").each(function(){
    alert($(this).val()+'-'+$(this).text());
    });


    }

If I got it right: 如果我做对了:

...
for(i=0; i<lnk.length;i++)
{

    $(select1).append($("<option>").val(val[i]).text(lnk[i]));
}

...

How about using it like this 这样使用它怎么样

for(i=0; i<lnk.length;i++)
{
   var my_option = '<option>'+text(lnk[i])+'</option>';
   $('#selection_id').html(my_option);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM