使用jquery获取下一个最接近的select元素

我想得到下一个select元素(因为会有多个具有相同id的代码),这里是代码

function fillProds(catid) { $.get("http://localhost/crops/?module=from-receipt&action=getproducts&format=json&catid="+catid , null,function(d){ CC = eval(d); t = $(this).find('select'); alert(t); t.options.length=0; t.options[0]=new Option("",""); for(i=0;i<CC.length;i++) { t.options[i+1]=new Option(CC[i].name,CC[i].id); } }); } 

而对于HTML部分

      $r ) { ?> <option value="">';             x   

所以问题是我得到的错误就像设置未定义的长度可能是因为我没有正确获取选择元素。 对上述内容进行任何修正,以便我可以正常工作并选择下一个选择元素?

谢谢。

你是这个意思吗?

http://api.jquery.com/eq-selector/

在jQuery中,您可以使用选择器,如$("select").next()$("select:eq(1)")

好吧,我有一个解决方案

 function fillProds(catid,obj) { $.get("http://localhost/crops/?module=from-receipt&action=getproducts&format=json&catid="+catid , null,function(d){ CC = eval(d); trs = $(obj).closest('tr'); t = trs.find('#prods').get(0); t.options.length = 0; t.options[0]=new Option("",""); for(i=0;i           x   

所以目前它正在寻找最近的tr并获得具有id prods的FIRST元素(我认为)

尝试删除内联绑定并通过jquery执行。

           x  

这是你的新js:

  $('.s-parent').on('change',function() { var $el =$(this), catid = $el.val(); $.get("http://localhost/crops/?module=from-receipt&action=getproducts&format=json&catid="+catid , null,function(d){ CC = eval(d); t = $('select',$el.closest('tr')).eq(1); //the second select in the same tr alert(t); t.options.length=0; t.options[0]=new Option("",""); for(i=0;i 

最后,这些是主要的变化

  var $el =$(this), catid = $el.val(); t = $('select',$el.closest('tr')).eq(1); //the second select in the same tr 

另外,我强烈建议你避免使用eval(d) 。 Insted使用jquery.getJson