简体   繁体   English

如何从asp.net中动态生成的html文本框中获取值

[英]How to get the values from dynamically generated html textboxes in asp.net

I want to fetch the values from dynamically created html textboxes in my aspx page. 我想从我的aspx页面中动态创建的html文本框中获取值。 Now first I want to check if the textbox exists or not. 现在首先我要检查文本框是否存在。 If exists then I want to fetch values from these texboxes. 如果存在,那么我想从这些texbox中获取值。 Here is the code which I am using for creating textboxes dynamically 这是我用来动态创建文本框的代码

<script type="text/javascript">
    var counter = 2;
    $(document).ready(function () {

        $("#addButton").click(function () {
            if (counter > 3) {
                alert("Limit Exceeds");
                return false;
            }
            var $wrap = $('#TextBoxesGroup');
            var dynamichtml = '<div id="div' + counter + '"><div class="mcity"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/>  </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 1 + '" class="auto"/> </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 11 + '" class="auto"/> </div>';
            $wrap.append(dynamichtml);
            counter++;
        });

        $("#removeButton").click(function () {
            if (counter == 1) {
                alert("No more textbox to remove");
                return false;
            }

            counter--;
            $("#TextBoxesGroup").find("#div"+ counter).remove();
          //  $("#TextBoxesGroup").find("#textbox" + counter+1).remove();


        });
  $(".auto").live("focus", function () {

                  $(this).autocomplete({

                      source: function (request, response) {
                          var textval = request.term; // $(this).val();
                          $.ajax({
                              type: "POST",
                              contentType: "application/json; charset=utf-8",
                              url: "Home.aspx/GetAutoCompleteData",
                              data: "{'code':'" + textval + "'}",
                              dataType: "json",
                              success: function (data) {
                                  response(data.d);
                              },
                              error: function (result) {
                                  alert("Error");
                              }
                          });
                      }
                  });
              });
          });

</script>

This code is generating textboxes generating and I am fetching data from my database using json autocomplete process.After this I am trying to send the information to another page using javascript the code that I write for this is below 这段代码正在生成文本框,并且我正在使用json自动完成过程从数据库中获取数据。之后,我尝试使用javascript将信息发送到另一页,我为此编写的代码如下

<script type="text/javascript">
      $(function () {
          $("#btnPost").bind("click", function () {
              //Create a Form
              var $form = $("<form/>").attr("id", "data_form")
                            .attr("action", "Complete.aspx")
                            .attr("method", "post");
              $("body").append($form);

              //Append the values to be send
              AddParameter($form, "txtleft", $("#txtSearchleft").val());
              AddParameter($form, "txtright", $("#txtSearchright").val());

              //Send the Form
              $form[0].submit();
          });
      });
      function AddParameter(form, name, value) {
          var $input = $("<input />").attr("type", "hidden")
                                .attr("name", name)
                                .attr("value", value);
          form.append($input);
      }
    </script>

This work fine for textboxes which placed default in my page. 对于在我的页面中放置默认值的文本框,此功能正常。 But for the textboxes that generate dynamically after these textboxes I dnt know how to send values of those textboxes to my next page 但是对于在这些文本框之后动态生成的文本框,我不知道如何将这些文本框的值发送到我的下一页

Javascript and asp.net experts please help me to resolve this Javascript和asp.net专家请帮助我解决此问题

Thanks 谢谢

You can get the controls values using FormCollection . 您可以使用FormCollection获取控件的值。 Note that it works on name rather than Id. 请注意,它适用于名称而不是ID。

You need to keep the count and follow some naming convention for name of your controls. 您需要保持计数,并遵循一些命名约定来命名控件。

Create a asp.net hidden field and keep the control count in that. 创建一个asp.net隐藏字段,并在其中保留控件数。 And get according the values of the hidden field. 并根据隐藏字段的值获取。
Msdn link MSDN链接

well if you were to assign your form an id you could just serialize your form and post it using ajax if you wanted 好吧,如果您要为表单分配一个ID,则可以序列化表单,然后使用ajax将其发布

$.post("complete.aspx",$("#data_form").serialize(),function(response){
//react to the post here
});

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

相关问题 如何获取动态生成的多个文本框的值? - How to get values of multiple textboxes generated dynamically? 如何获取动态生成的元素 ASP.NET 的属性 - How to get attribute of dynamically generated element ASP.NET 如何使用JavaScript在ASP.NET Web表单上动态添加文本框 - How to add textboxes dynamically on a asp.net webform using javascript 如何在JavaScript中使用具有相同ID的两个文本框获取asp.net用户控件的值 - How to get the values of an asp.net user control with two textboxes with same id in javascript 将动态生成的文本框值传递给ASP.NET MVC中的控制器 - Passing dynamically generated textbox values to controller in ASP.NET MVC 如何获取动态生成的文本框的输入值? - how to get the input value of dynamically generated textboxes? 从HTML div启动JavaScript函数,其中函数名是动态生成的ASP.NET - Launch JavaScript function from HTML div where function name is generated dynamically ASP.NET 如何从动态生成的文本框中保存数据? - How to save data from dynamically generated textboxes? 如何从JSON中动态创建的文本框行中获取值 - How to get the values from dynamically created row of textboxes in JSON 如何使用 JavaScript 从 Html.BeginForm() ASP.NET MVC 获取值 - How to get values using JavaScript from Html.BeginForm() ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM