【问题标题】:How to get the id of a drop down list from different rows of a table?如何从表的不同行中获取下拉列表的 id?
【发布时间】:2018-10-16 22:35:34
【问题描述】:

我目前正在开发一个网络应用程序,该应用程序允许用户使用 asp.net C# 创建类似于 Google 表单的调查。现在无法获取生成的下拉列表的 id,这就是为什么我不能在新行中放置任何字段的原因是表单的代码:

  <div class="form-group" style="margin:auto; width:80%;">
        <form name="add_question" id="add_question">
            <asp:Label ID="Label1" runat="server" Text="Survey Title: " Font-Size="Large"></asp:Label>
            <input type="text" name="title" id="title" class="form-control question_list" placeholder="Enter Survey Title"/>
            <asp:Label ID="Label2" runat="server" Text="Description: " Font-Size="Large"></asp:Label>
            <br>
            <textarea rows="4" cols="100%" name="description" id="description"  placeholder="Enter description"></textarea>
            <table class="table" id="dynamic_field">
                <tr>
                    <th style="width: 50%">Question</th>
                    <th style="width: 15%">Type of Question</th> 
                    <th style="width: 30%">Fields</th>
                    <th style="width: 5%">Remove</th>
                </tr>
                <tr id="row1">
                    <td style="width: 50%"><input type="text" name="question1" id="question1" class="form-control question_list" placeholder="Enter question"/></td>
                    <td style="width: 15%"><select name="type1" id="type1" class="dropdown">
                            <option selected></option>
                            <option value="1">Multiple Choice</option>
                            <option value="2">CheckBox</option>
                            <option value="3">Short Sentence</option>
                            <option value="4">Paragraph</option>
                            <option value="5">Line Scale</option>
                        </select></td> 
                    <td style="width: 30%"></td>
                    <td style="width: 5%"> <button name="remove" id="1" class="btn btn-danger btn_remove"> X </button></td>

                </tr>
            </table>
            <button type="button" name="add" id="add" class="btn btn-warning" > Add Question </button>
        </form>
        <button type="button" name="submit" id="submit" class="btn btn-success" > Submit </button>
    </div>  

这里是生成我正在进行的调查字段的代码:

 <script>
    var i = 1;

        $("#add").click(function () {

            i++;
            $("#dynamic_field").append('<tr id="row' + i + '"> <td> <input type="text" name="question' + i + '" id="question'
                + i + '" class="form-control question_list" placeholder="Enter question"/> </td><td><select name="type'
                + i + '" id="type' + i + '">'
                + '<option selected></option>'
                + '<option value="1">Multiple Choice</option>'
                + '<option value="2">CheckBox</option>'
                + '<option value="3">Short Sentence</option>'
                + '<option value="4">Paragraph</option>'
                + '<option value="5">Line Scale</option>'
                + '</select></td><td></td><td><button name="remove' + i + '" id="'
                + i + '" class="btn btn-danger btn_remove"> X </button>  </td> </tr> ');
        });

        $(document).on('click', '.btn_remove', function () {
            var button_id = $(this).attr("id");
            $("#row" + button_id + "").remove();
        });

        $('#type1').change(function () {
            var x = 1;
            if ($(this).val() == '3') {
                $('#myInput' + x + '').remove();      
                var row = document.getElementById("row1");
                var z = row.insertCell(2);        
                z.innerHTML = '<td style="width: 30%"><input id="myInput' + x + '" type="text" style="width: 80%" /></td>';
                row.deleteCell(3);
            }
            else if ($(this).val() == '4') {
                $('#myInput' + x + '').remove();
                var row = document.getElementById("row1");
                var z = row.insertCell(2);
                z.innerHTML = '<td style="width: 30%"><textarea rows="2" cols="40" name="description" id="myInput' + x + '"></textarea></td>';
                row.deleteCell(3);
            } else {
                $('#myInput'+x+'').remove();
            }

        });

</script>

我也愿意接受任何可以使其更容易创建的想法。

【问题讨论】:

  • 我不确定这里有什么问题。您已经在选择生成的&lt;select&gt; 元素的 id:$('#type1')。你可以得到其他索引不同的:$('#type' + i)
  • 我已经尝试过这样做,但我无法像第一行那样生成新字段
  • 这很奇怪,因为我尝试了您的代码并且它运行良好。使用添加按钮,我可以添加新行,使用删除按钮删除它们。
  • 与@LudovitMydla 相同。我不明白这个问题。 :) 但无论如何,您的代码可以得到增强。也许我会尝试一下。

标签: javascript jquery asp.net forms html-table


【解决方案1】:

创建表格

<table class="table" id="dynamic_field">
   <thead>
       <tr>
           <th style="width: 50%">Question</th>
           <th style="width: 15%">Type of Question</th> 
           <th style="width: 30%">Fields</th>
           <th style="width: 5%">Remove</th>
       </tr>
   </thead>
   <tbody>
   </tbody>
</table>

追加行时,追加到tbody。

$("#add").click(function () {

        i++;
        $("#dynamic_field tbody").append('<tr id="row' + i + '"> <td> <input type="text" name="question' + i + '" id="question'
            + i + '" class="form-control question_list" placeholder="Enter question"/> </td><td><select name="type'
            + i + '" id="type' + i + '">'
            + '<option selected></option>'
            + '<option value="1">Multiple Choice</option>'
            + '<option value="2">CheckBox</option>'
            + '<option value="3">Short Sentence</option>'
            + '<option value="4">Paragraph</option>'
            + '<option value="5">Line Scale</option>'
            + '</select></td><td></td><td><button name="remove' + i + '" id="'
            + i + '" class="btn btn-danger btn_remove"> X </button>  </td> </tr> ');
    });

像这样更改更改事件

$('#dynamic_field').on('change','[id^=type]',function () {
        var x = 1;
        if ($(this).val() == '3') {
            $(this).closest('tr').find('[id^=myInput]').remove();      
            var row = $(this).closest('tr');
            var z = row.insertCell(2);        
            z.innerHTML = '<td style="width: 30%"><input id="myInput' + x + '" type="text" style="width: 80%" /></td>';
            row.deleteCell(3);
        }
        else if ($(this).val() == '4') {
            $(this).closest('tr').find('[id^=myInput]')
            var row = $(this).closest('tr');
            var z = row.insertCell(2);
            z.innerHTML = '<td style="width: 30%"><textarea rows="2" cols="40" name="description" id="myInput' + x + '"></textarea></td>';
            row.deleteCell(3);
        } else {
            $(this).closest('tr').find('[id^=myInput]')
        }

    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多