2012-09-21 23 views
2

我很難在我的中繼器控件中查找文本框值。在按鈕點擊我需要獲得文本框對應的按鈕被點擊的文本框。使用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()); 
}); 

那麼點,你正在自定義屬性「名稱」,然後搜索具有相同屬性名稱的文本框。

+0

你能顯示你的html結構嗎?他們都是兄弟姐妹還是隻是相關元素的兄弟姐妹? –

+0

你可以用html和jquery創建一個js小提琴嗎?它會在嘗試調試時幫助加載。 – Burt

+0

fiddler:http://jsfiddle.net/TcSWV/17/ – Timsen

回答

2

你可以用服務器上的某些屬性來標記它們,並在你的jQuery代碼中使用它。

從服務器的輸出會是這樣的:

<input type='text' data-item-id='1'>First</input> 
<inpyt type='button' data-item-id='1'></input> 

<input type='text' data-item-id='2'>Second</input> 
    <inpyt type='button' data-item-id='2'></input> 

後,一旦按下按鈕,得到它的數據項-id和搜索具有相同屬性值的文本。

+0

我從代碼behinde動態加載中繼器,你可以顯示如何添加數據項目ID? – Timsen

+0

想通了,你引導我在正確的方向。 – Timsen

1

評論後編輯。

我前往您的jsfiddle,使其與這方面的工作:

$('[id*="answerQuestion"]').click(function() { 
    $(this).parent().children('[id*="TextArea_' + $(this).attr('id').substring($(this).attr('id').length - 1) + '"]').val('clicked'); 
}); 

說明:

  1. 我意識到,你的IDS其中answerQuestion的按鈕和TextArea爲TXT。
  2. 在我犯了一個選擇誰與文本區域ID的運作功能(子添加 指數的話)

這就是它!這有點棘手,但它是我找到的最佳解決方案。

希望這會有所幫助!

問候,對不起,如果有一些英語不一致:)!

+0

我不能這樣做,因爲ID是動態的 – Timsen