2

リピーターコントロールでテキストボックスの値を見つけるのに苦労しています。ボタンをクリックすると、クリックされたボタンの内側にあるテキストボックスに対応するテキストボックス値を取得する必要があります。

ので、私は持っています :

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関数を持っています:

 $(".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);
    });

私はそれを部分的に動作させました:

$(this).siblings('input[type="text"]').first().val("clicked");

テキストボックスのテキストを設定しましたが、2番目のボタンをクリックしても何も起こりません。

何が間違っているのですか?特定のテキストボックスから値を取得するにはどうすればよいですか?

アップデート

今、私は各コ​​ントロールにカスタム属性を追加するソリューションを使用します:

    <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");
});

しかし、ボタンとしてsammeカスタム属性名を持つテキストボックスコントロールを取得するにはどうすればよいですか?

アップデート2

70回試行した後、私はついにそれを理解しました(Jqueryでの最初の試行)

解決

$(".addAnswerButton").click(function() {
    var pos = $(this).attr("mydataid");
    results = $('input[type = "text"][mydataid="'+pos+'"]').first();
    alert(results.val());
});

つまり、カスタム属性「name」を取得してから、同じ属性名のテキストボックスを検索しているということです。

4

2 に答える 2

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>

その後、ボタンが押されたら、そのdata-item-idを取得し、同じ属性値を持つテキストボックスを検索します。

于 2012-09-21T14:00:21.443 に答える
1

コメントの後に編集。

私はあなたのjsfiddleに向かい、これで動作させます:

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

説明:

  1. ボタンのanswerQuestionとtxtのTextAreaのIDがわかります。
  2. 関数で、TextArea IDを操作するセレクターを作成しました(サブストリングはそれにインデックスを追加します)

それでおしまい!少し注意が必要ですが、私が見つけた最良の解決策です。

お役に立てれば!

ご挨拶、英語の不一致がある場合は申し訳ありません:)!

于 2012-09-21T14:16:55.890 に答える