.net 初学者。学习笔记 [html的<select>下拉标签option赋值]

前台:

<td>
    <asp:HiddenField ID="hidVehTypeId" runat="server" />
    <asp:HiddenField ID="hidVehTypeName" runat="server" />
    <select id="vehType1" name="vehType1">
     </select>
</td>

后台(此方法须在页面加载是调用):

        /// <summary>
        /// 查询并把值赋予隐藏域
        /// </summary>
        public void SelectVehType()
        {
            IList<RJ.Model.data_dictionary> dictionData = Business.UserInfo.SelectVehType();
            string vehTypeId = "";
            string vehTypeName = "";
            foreach (var item in dictionData)
            {
                vehTypeId += "," + item.data_key;
                vehTypeName += "," + item.data_value;
            }

            this.hidVehTypeId.Value = vehTypeId.Substring(1);
            this.hidVehTypeName.Value = vehTypeName.Substring(1);
        }

JS:

    <script type="text/javascript">
        function SelectVehType() {
            var typeid = $("#hidVehTypeId").val();
            var typeidsil = typeid.split(",");
            var typename = $("#hidVehTypeName").val();
            var typenamesil = typename.split(",");

            for (var i = 0; i < typeidsil.length; i++) {
                var sel = document.getElementById('vehType1');
                var opt = document.createElement('option');
                opt.setAttribute('value', typeidsil[i]);
                opt.innerText = typenamesil[i];
                sel.appendChild(opt);

            }
        }
    </script>
分享到: 更多
原文地址:https://www.cnblogs.com/nanmuhigh/p/2654408.html