2015-04-22 37 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

相關問題