簡體   English   中英

如何從asp.net中動態生成的html文本框中獲取值

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

我想從我的aspx頁面中動態創建的html文本框中獲取值。 現在首先我要檢查文本框是否存在。 如果存在,那么我想從這些texbox中獲取值。 這是我用來動態創建文本框的代碼

<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>

這段代碼正在生成文本框,並且我正在使用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>

對於在我的頁面中放置默認值的文本框,此功能正常。 但是對於在這些文本框之后動態生成的文本框,我不知道如何將這些文本框的值發送到我的下一頁

Javascript和asp.net專家請幫助我解決此問題

謝謝

您可以使用FormCollection獲取控件的值。 請注意,它適用於名稱而不是ID。

您需要保持計數,並遵循一些命名約定來命名控件。

創建一個asp.net隱藏字段,並在其中保留控件數。 並根據隱藏字段的值獲取。
MSDN鏈接

好吧,如果您要為表單分配一個ID,則可以序列化表單,然后使用ajax將其發布

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM