1

次の構成でループを実行する際に問題が発生しました。

<div id="items">
    <p id="test1">
        <select  name="1" id="1"><option>A</option><option selected>B</option></select>
        <input type="text" name="to" id="to">
        <input type="text" name="away" id="awy">
    </p>
    <p id="test2">
        <select  name="2" id="2"><option>A</option><option selected>B</option></select>
        <input type="text" name="to" id="to">
        <input type="text" name="away" id="awy">
    </p>
    <p id="test3">
        <select  name="3" id="3"><option>A</option><option selected>B</option></select>
        <input type="text" name="to" id="to">
        <input type="text" name="away" id="awy">
    </p>
</div>

test1、test2、test3を実行し、選択したオプションとテキストフィールドの値(select、to、away)を読み取る必要があります。たとえば、test1で1つしか取得できなかった場合にこれを行うと、クエリに問題はありません。

$('#items').children('p').each(function () {...}

しかし、2つのテキストフィールドを追加し、テストごとにこれを実行したい場合(1〜3)、わかりません...

何か案が?ありがとう!

4

4 に答える 4

3

IDは、DOM内の一意のアイテムを表す必要があります。代わりに、次のようにクラスを使用します。

<input type="text" name="to" class="to">
<input type="text" name="away" class="awy">
于 2013-03-08T16:28:38.560 に答える
0

これはどう?

$('#items').find('select option:selected, input:text').each(function(){
      alert($(this).val()); //Value of each selected option and text field within the div
});
于 2013-03-08T16:28:27.950 に答える
0

あなたもその.children部分を行う必要はありません。

これを試して:

$('#items > p').each(function () {
    // you can then use $(this) to reference the currently iterated p element.
    // such as $(this).find('input[type="text"]:first');
    // or
    // $(this).find('input[type="text"]:last');
});
于 2013-03-08T16:28:50.673 に答える
0

注: まず、すべての要素に一意の ID を指定します。

次のコードが役立つ場合があります。

$(‘p’).each(function(index){
this.children().get(0).val();

//now do for second and third also
}
于 2013-03-08T16:35:44.887 に答える