2017-01-31 25 views
0

私はタイプとブランドと呼ばれるHTMLで2つの選択ボックスを持っています。型が変更されると、私はonchangeイベントを使用してサーバーへのajax要求を作成し、型フィールドで製品を生成するブランドを取得します。それはすべて正常に動作しているが、私はブランドの選択で取得したデータを読み込む必要があり、私はタイプとブランドの選択ボックスを含む複数の行を持っているので、それを行う方法がわからない。変更された型と同じ行の次の選択要素を見つけるにはどうすればよいですか?jQuery次の行の要素を選択

私のコード HTML:

<tr> 
<td> 
    <select id="type" onchange="on_part_type_changed(this)" name "type[]"> --[options]-- </select> 
</td> 
<td> 
    <select id="brand" name="brand[]"></select> 
</td> 
</tr> 

Javascriptを:

function on_part_type_changed(selectObject) { 
    // ajax call here (works) 

    // now need to update the brand select that is on the same row as selectObject (jQuery) 
} 
+0

このように、http://www.codexworld.com/dynamic-dependent-select-box-using-jquery-ajax-php/ – rahulsm

+0

'$(event.target).next( 'select').html( ...) 'または' $(this).next( 'select').html(...) ' – Rajesh

+1

' type'と 'brand'の複数のIDを持つことはできません – Justinas

答えて

1

は、ブランドのオブジェクトを保存してください。あなたはAjaxレスポンスが戻ってくると準備が整います。

function on_part_type_changed(selectObject) { 
    //clumsy way of finding the brand object nextdoor 
    var brandObj=selectObject.parent().next().children('.brand'); 
    // ajax call here 
    someKindOfAjax(response) { 
     //put response into brand object we got earlier 
     brandObj.html(response); 
    } 
} 

「ブランド」である複数の選択がある場合は、IDの代わりにクラスである必要があります。

1

あなたはAJAX呼び出しを行う前に、この

function on_part_type_changed(selectObject) { 
    $(selectObject).next('.brand').html('...'); 
} 
+0

' .brand' IDは一意でなければならない – Rajesh