我的 html 中有 2 个选择框,称为类型和品牌。当类型更改时,我使用 onchange 事件向服务器创建 ajax 请求,并检索在类型字段中生产产品的品牌。这一切都工作正常,但我需要在品牌选择中加载检索到的数据,但我不知道如何做到这一点,因为我有多行包含类型和品牌选择框。如何在更改类型的同一行上找到下一个选择元素?
我的代码 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)
}
最佳答案
在执行 ajax 调用之前保存品牌对象。一旦 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。
关于javascript - jQuery 下一个选择行上的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41957803/