如何动态添加DropdownList。 。 。 [英] How To Add DropdownList Dynamiclly. . .

查看:52
本文介绍了如何动态添加DropdownList。 。 。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这里我的问题是如何在Javascript中动态添加DDL



我的JS代码



Hi,

Here my problem is How to Add DDL Dynamiclly in Javascript

My JS Code

<script type="text/javascript">
        function GetDynamicTextBox(value)
        {
            return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />' +
                    '<input type="button" value="Remove" onclick = "RemoveTextBox(this)" />'
        }
        function AddTextBox() {
            var div = document.createElement('DIV');
            div.innerHTML = GetDynamicTextBox("");
            document.getElementById("TextBoxContainer").appendChild(div);
        }

        function RemoveTextBox(div) {
            document.getElementById("TextBoxContainer").removeChild(div.parentNode);
        }

        function RecreateDynamicTextboxes() {
            var values = eval('<%=Values%>');
            if (values != null) {
                var html = "";
                for (var i = 0; i < values.length; i++) {

                    html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
                }
                document.getElementById("TextBoxContainer").innerHTML = html;
            }
        }
        window.onload = RecreateDynamicTextboxes;
    </script>





Html代码





Html Code

<form id="form1" runat="server">
    <input id="btnAdd" type="button" value="add" onclick="AddTextBox()" />
    <select id="Select1"><option>Item1</option></select>
    <br />
    <br />
    <div id="TextBoxContainer">
        <!--Textboxes will be added here -->
    </div>
    <br />
    <asp:Button ID="btnPost" runat="server" Text="Post" OnClick="Post" />
    </form>





如何动态地在此部分添加Dropdownlist .. ..



谢谢....



How to add Dropdownlist in this part dynamically ....

Thanks....

推荐答案

这是在jQuery中动态绑定下拉列表的示例。



假设我们有一个id为'dpdTest'的下拉列表如下: -

This is the example for binding dropdown dynamically in jQuery.

Let say we have a dropdown with id 'dpdTest' as below :-
<select id="dpdTest"></select>





现在让我说我拥有所有值JavaScript数组,我想使用这个数组值填充下拉列表。





Now let say i have all the values in an JavaScript array and i want to populate the dropdown using this array values.

function BindTestDPD() {


#dpdTest)。append( < option value = 0 >选择DPD< / option>);
var vals = [ Item1 Item2 Item3];
("#dpdTest").append("<option value="0">Select DPD</option>"); var vals = ["Item1", "Item2", "Item3"];


.each(vals, function (key,val){
.each(vals, function (key, val) {


这篇关于如何动态添加DropdownList。 。 。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆