2015-04-22 99 views
0

我用jquery代码创建了自动文本框。但我无法从他们那里获得价值。这里是我的代码来生成文本框如何从自动生成的asp.net中的多个texboxes中获取价值?

$('#btn').click(function() { 
$(container).append('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;"> 
<b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag"> 
<input type=text class="input" id=txtdates' + iCnt + ' ' + ' /></td> 
</tr>'); 
} 

我可以添加此代码的文本框。我如何从这些代码中获得价值?

回答

1

首先导入这个命名空间

using System.Web.Script.Serialization; 

下一页添加属性叫名字你的动态创建文本框,如下

$('#btn').click(function() { 
     $(container).append('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;"> 
      <b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag"> 
      <input type="text" name="DynamicTextBox" class="input" id="txtdates"' + iCnt + ' ' + ' /> 
      </td> 
      </tr>'); 
} 

在你的服务器的方法,你可以访问它,如下

public void Post(object sender, EventArgs e) 
{ 
    string[] textboxValues = Request.Form.GetValues("DynamicTextBox"); 
    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    this.Values = serializer.Serialize(textboxValues); 
    string message = ""; 
    foreach (string textboxValue in textboxValues) 
    { 
     message += textboxValue + "\\n"; 
    } 
} 

Source

+1

谢谢。这是工作。非常感谢你! – enderaric

+0

随时!快乐编码.. :) –

0

这是我对您的问题takeon:

$('#btn').click(function() { 
    var iCnt = 1; 
    $('#container').append($('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;"><b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag"><input type="text" class="input" id="txtdates' + iCnt + '" ' + ' /></td></tr>')); 
}); 

$('#btnget').click(function() { 
    alert($('#container').find('input:text').val()); 
}); 

<div id='container'></div> 
<input type='button' id='btn' value='submit'/> 
<input type='button' id='btnget' value='get value'/> 

演示:https://jsfiddle.net/hxq5cj0x/

+0

是的,我可以从JavaScript的值,但不知道如何做到这一点的asp.net代码。因为我会将它们写入数据库 – enderaric

+0

@enderaric在上面的代码中添加一个asp.net hiddenfield,而不是'alert'将文本值绑定到隐藏字段。然后,在代码隐藏中读取隐藏字段的值? – renakre

+0

我正在尝试你的答案。我会在这里写出结果。 – enderaric

相关问题