jquery或者js对html控件的处理汇总

1、下拉列表select的处理

a)、后台通过jquery获取的json数据对下拉列表select的赋值操作:

html页面:<select name="gameserverlist" id="gameserverlist"  ></select>

function JqueryAjaxFun() {
        $.ajax({
            url: "Home/GetGameServerListData",
            type: "get",
            dataType: "json", //表示纯文本
            data: {},
            success: function (data, status) {
                for (var i = 0; i < data.length; i++) {
                    $("#gameserverlist").append("<option value='" + data[i].server_id + "'>" + data[i].server_name + "</option>");
                }
            },
            error: function () {//失败
                alert('Ajax调用失败!');
            }
        });
    }

 js获取当前选中的select值:

       var obj = document.getElementById("gameserverlist"); //定位id
        var index = obj.selectedIndex; // 选中索引
        var text = obj.options[index].text; // 选中文本
        var value = obj.options[index].value; // 选中值
        alert(text + "[" + value + "]");

jquery获取当前选中的select值:

        var a = $("#gameserverlist  option:selected").text();
        var b = $("#gameserverlist  option:selected").val();
        alert(a + '|' + b);

原文地址:https://www.cnblogs.com/yonguibe/p/4758308.html