简体   繁体   English

获取动态添加的输入字段的内容

[英]Get content of dynamically added input fields

I am adding extra input fields to a webpage with JavaScript when a button is clicked and am trying to get the content of the new input fields in ASP.NET, but I am only able to access the original input fields and not the new ones created with JavaScript. 单击按钮时,我正在使用JavaScript向网页添加额外的输入字段,并尝试获取ASP.NET中新输入字段的内容,但我只能访问原始输入字段,而不能访问创建的新输入字段。使用JavaScript。

Original List that gets added to: 原始列表添加到:

<ul class="add-event-list" id="eventList" runat="server">
    <li id="eventItemsList">
         <asp:TextBox name="event-info" TextMode="MultiLine" Columns="50" Rows="5" id="eventInfo" placeholder="Enter an Event..." runat="server"/>

         <label for="event-date" class="field-heading">Date</label>
         <asp:TextBox Type="date" name="event-date" id="eventDate" placeholder="Enter a Date..." runat="server"/>
     </li>
</ul>
<button class="btn add-event-btn" onclick="return false" >Add Event <span class="glyphicon glyphicon-plus"/></button>

On the Button Click: 在按钮上单击:

$('.add-event-btn').on('click', function () {
    var list = $('#eventItemsList').clone();
    $('.add-event-list').append(list);
});

On the submit click: 在提交上,单击:

<button class="grey-btn" id="buttonSubmitHouse" onserverclick="buttonSubmitHouse_ServerClick" runat="server">Submit</button>

Only shows the original elements in the ul: 仅显示ul中的原始元素:

protected void buttonSubmitHouse_ServerClick(object sender, EventArgs e)
{
    foreach (Control item in eventList.Controls)
    {
        Control Test = item;
    }
}

The reason you're encountering the issue, with the dynamic control they aren't actually rendering on the server. 遇到问题的原因是,使用动态控件时,它们实际上并未在服务器上呈现。 Which is why you can't iterate through them, the server doesn't know they exist. 这就是为什么您无法遍历它们,服务器不知道它们存在的原因。 The easiest approach, would be to implement a Web Service. 最简单的方法是实现Web服务。

When you call the service you would simply append a QueryString , then perform your desired functionality. 调用服务时,您只需添加QueryString ,然后执行所需的功能即可。 So from the Client you would do: 因此,从客户那里您可以做:

$.ajax({
     type: '...',
     url: '...',
     dataType: 'json',
     success: (function (response) {
          // Do something with service response.
     })
});

Depending on your technology, you would either hit a Controller or use a Generic Asp Handler File, so you would anchor onto HttpContext that is passed to the handler. 根据您的技术,您将命中一个控制器或使用一个通用Asp处理程序文件,因此您将锚定到传递给该处理程序的HttpContext上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM