我很難在我的中繼器控件中查找文本框值。在按鈕點擊我需要獲得文本框對應的按鈕被點擊的文本框。使用Jquery動態查找中繼器中的控制
所以我有:
Textbox1的--->這必須與Button1的 TextBox2中---->這勢必與Button2的
<span>Question :</span><span>my second content <br /></span><span>Answer : </span><input name="ctl00$MainContent$ctl00$ctl00$TextArea" type="text" id="MainContent_ctl00_TextArea_0" value="6454" />
添加問題
約束問題:abcdedf
答案:加問題
現在我有jquery函數,它得到了whic h被點擊:
$(".addAnswerButton").click(function() {
//var quantityBox = $("#TextArea", $(this).parent());
var tr = $(this).closest("text")
alert(tr.val());
//quantityBox.text = "Hello there";
//var currentValue = quantityBox.val();
$(this).siblings('input[type="text"]').first().val("clicked");
//quantityBox.val(parseInt(currentValue) + 1);
});
Ive得到了它與部分工作:
$(this).siblings('input[type="text"]').first().val("clicked");
從哪裏獲得設置文本框的文本,但話又說回來,如果我點擊第二個按鈕,沒有任何happends。
我做錯了什麼?我如何從特定的文本框中獲得價值?
UPDATE
現在我去與解決方案,我自定義屬性添加到每個控制:
<span>Question :</span>
<span>my second content <br /></span>
<span>Answer : </span>
<input name="ctl00$MainContent$ctl00$ctl00$TextArea" type="text" id="MainContent_ctl00_TextArea_0" value="654" mydataid="2" />
<button id="MainContent_ctl00_answerQuestion_0" Class="addAnswerButton" onclick="Click()" mydataid="2" >Add Question</button><hr /><span>Question :</span><span>abcdedf <br /></span>
<span>Answer : </span><input name="ctl00$MainContent$ctl00$ctl01$TextArea" type="text" mydataid="1" id="MainContent_ctl00_TextArea_1" value="654" />
<button id="MainContent_ctl00_answerQuestion_1" Class="addAnswerButton" onclick="Click()" mydataid="1">Add Question</button>
所以,現在我可以這樣做,從onclick事件得到自定義屬性附加傷害:
$(".addAnswerButton").click(function() {
var pos = $(this).attr("mydataid");
alert(pos);
// $(this).siblings('input[type="text"]').first().val("clicked");
});
但是,我如何獲得文本框控件哪些作爲按鈕的SAM自定義屬性名稱?
更新2
後70個tryes我終於想通了(我用jQuery第一tryes)
解決方案
$(".addAnswerButton").click(function() {
var pos = $(this).attr("mydataid");
results = $('input[type = "text"][mydataid="'+pos+'"]').first();
alert(results.val());
});
那麼點,你正在自定義屬性「名稱」,然後搜索具有相同屬性名稱的文本框。
你能顯示你的html結構嗎?他們都是兄弟姐妹還是隻是相關元素的兄弟姐妹? –
你可以用html和jquery創建一個js小提琴嗎?它會在嘗試調試時幫助加載。 – Burt
fiddler:http://jsfiddle.net/TcSWV/17/ – Timsen