怎样给动态添加的select赋值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样给动态添加的select赋值相关的知识,希望对你有一定的参考价值。

参考技术A 触发获取动态数据的方法
获取动态数据list
清除select下面所有的选项
把获取的数据append到select下面
$('#id).change(function()
var val = this.value;
document.getElementById("selectId").options.length = 0;
if(val != null && val != '')
$.post(url, "temp": val, function(data)
if(data != null && data.length > 0)
for(var i=0; i<data.length; i++)
var o = data[i];
$("#selectId").append("<option value='"+o[0]+"'>"+o[1]+"</option>");


$("#selectId").selectmenu('refresh', true);//jqm 是动态加载的css 所以新增元素后 需要手动加载样式
);

);本回答被提问者和网友采纳

js 获取select的值 / js动态给select赋值

var t = document.getElementById("select1");
var selectValue=t.options[t.selectedIndex].value;//获取select的值

var t1 = document.getElementById("select2");

for(i=0;i<t1.length;i++){//给select赋值
    if(selectValue==t.options[i].value){
        t1.options[i].selected=true
    }
}

第二种方法,赋值是一样

<select id="select1" name="select1">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option> 
</select>
<input type="button" id="button1" value="submit" οnclick="getSelectValue()"/>
<script>
function getSelectValue(){
    var selectCount = document.getElementById("select1").options;
    for(var i = 0 ; i<selectCount.length;i++){
        if(selectCount[i].selected){
            alert(selectCount[i].value);
        }
    }
}
</script>

文章转自:https://blog.csdn.net/yy_2011/article/details/7885469

以上是关于怎样给动态添加的select赋值的主要内容,如果未能解决你的问题,请参考以下文章

求大侠帮忙看看js不能给父页面的select动态添加option的问题

JS如何动态给select的option赋值?

js 获取select的值 / js动态给select赋值

js 获取select的值 / js动态给select赋值

js中给select 添加options

怎么在动态添加select下拉框中 选择一个默认的option