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

时间:2012-06-15 12:14:23

标签: jquery

我想获得下一个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部分

<tr>
                <td class="first" width="172">
                    <select onchange="fillProds(this.value)" id="catid">
                        <option></option>
                        <?php foreach ( $cats as $v => $r ) { ?>
                            <option value="<?=$r['id']?>"><?=$r['name']?></option>';
                        <?php } ?>
                    </select>
                <td>
                    <input type="hidden" name="transindetid[]" value="" />
                    <select name="transindet[prodid][]" id="prods" class="validate-not-first">
                        <option></option>
                    </select>
                </td>
                <td class="last">
                    <input type="text" name="transindet[qty][]" class="required" value=""> &nbsp;
                    <a href="#" class="delRow" style="color:red">x</a>
                </td>
</tr>

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

谢谢。

3 个答案:

答案 0 :(得分:1)

这是你的意思吗?

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

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

之类的选择器

答案 1 :(得分:0)

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

<td class="first" width="172">
                <select class="s-parent" id="catid">
                    <option></option>
                    <?php foreach ( $cats as $v => $r ) { ?>
                        <option value="<?=$r['id']?>"><?=$r['name']?></option>';
                    <?php } ?>
                </select>
            <td>
                <input type="hidden" name="transindetid[]" value="" />
                <select name="transindet[prodid][]" id="prods" class="validate-not-first">
                    <option></option>
                </select>
            </td>
            <td class="last">
                <input type="text" name="transindet[qty][]" class="required" value=""> &nbsp;
                <a href="#" class="delRow" style="color:red">x</a>
            </td>

这是你的新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<CC.length;i++) {
            t.options[i+1]=new Option(CC[i].name,CC[i].id);
        }
    });
});

最后,这些是主要变化

 var $el =$(this),
        catid = $el.val();

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

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

答案 2 :(得分:0)

好的,我有一个解决方案

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<CC.length;i++) {
            t.options[i+1]=new Option(CC[i].name,CC[i].id);
        }
    });
}

<tr>
                <td class="first" width="172">
                    <select onchange="fillProds(this.value,this)" id="catid">
                        <option></option>
                        <?php foreach ( $cats as $v => $r ) { ?>
                            <option value="<?=$r['id']?>"><?=$r['name']?></option>';
                        <?php } ?>
                    </select>
                <td id="select">
                    <input type="hidden" name="transindetid[]" value="" />
                    <select name="transindet[prodid][]" id="prods" class="validate-not-first">
                        <option></option>
                    </select>
                </td>
                <td class="last">
                    <input type="text" name="transindet[qty][]" class="required" value=""> &nbsp;
                    <a href="#" class="delRow" style="color:red">x</a>
                </td>
</tr>

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

相关问题