[英]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.