js给下拉框赋值

JavaScript07

js给下拉框赋值,第1张

法1.$("#Select").append("<option value='"+id+"'>"+name+"</option>")

法2.从json读取

<input class="easyui-combobox" name="" id="" data-options="valueField:'id',textField:'name',url:'json文件或AJAX事件'" />

var opt = '<option value="1">呵呵1</option><option value="2">呵呵2</option>'

$('#id').html(opt)

id就是select标签的id值,这是用jQuery

document.getelementbyid(id).html=opt js方式

首选jQuery

<form aciton="../php/register.php" method="post">

        <p>

            <label style="color: #ffffffwidth: 40%text-align: right">所&nbsp&nbsp在&nbsp&nbsp省:</label>

            <select style="width:75%" id="s_province" name="s_province" onchange="setVal1(this.options[this.selectedIndex].text)"></select>

        </p>

        <p>

            <label style="color: #ffffffwidth: 40%text-align: right">所&nbsp&nbsp在&nbsp&nbsp市:</label>

            <select style="width:75%" id="s_city" name="s_city" onchange="setVal2(this.options[this.selectedIndex].text)"></select>

        </p>

        <p>

            <label style="color: #ffffffwidth: 40%text-align: right">所&nbsp&nbsp在&nbsp&nbsp县:</label>

            <select style="width:75%" id="s_county" name="s_county" onchange="setVal3(this.options[this.selectedIndex].text)"></select>

        </p>

            <input type="hidden" id="shengText" value=""  name="shengText"/>

            <input type="hidden" id="xianText" value=""  name="xianText"/>

            <input type="hidden" id="shiText" value=""  name="shiText"/>

            <script class="resources library" src="http://sandbox.runjs.cn/uploads/rs/267/g3ugugjp/area.js" type="text/javascript"></script>

            <script type="text/javascript">_init_area()</script>

        <p><button type="submit" class="button1">注 册</button>

        </p>

    </form>

</div>

</body>

<script type="text/javascript">

    var Gid  = document.getElementById 

    var showArea = function(){

        Gid('show').innerHTML = "<h3>省" + Gid('s_province').value + " - 市" +

        Gid('s_city').value + " - 县/区" +

        Gid('s_county').value + "</h3>"

    }

    Gid('s_county').setAttribute('onchange','showArea()')

</script>

<script  language="javascript">

    function setVal1(_text)

    {

        document.getElementById("shengText").value=_text

    }

    function setVal2(_text)

    {

        document.getElementById("shiText").value=_text

    }

    function setVal3(_text)

    {

        document.getElementById("xianText").value=_text

    }

</script>

这样我是能获取到值, 但是只获取到最后一个 县的那个 select值。  试了半个小时。发现是js文件为select赋值的原因。 有没有什么其他办法。 既可以用js文件为select赋值,还能获取到值为文本框赋值。