开发者

Problem with jQueryscript selector

I try to write at jQuery-script who catch "Enter"-keypress and find the first A-tag who is on the same div with class "findMva" and who is not on an other child div with class "findMva".

Example 1:

  • I have focus on input with id="input2"
  • I press Enter
  • I want the script click on the A-tag with text "More info 9..."

Example 2

  • I have focus on input with id="input4"
  • I press Enter
  • I want the script click on the A-tag with text "More info 5..."

jQuery-script (who not is working on example 1, but not on example 2...):

$("input").keydown(function (e) {  
    if (e.which == 13) {  
        $(this).closest(".findMva").find(".mva").not(".findMva > .mva").first().click();  
    }  
}

HTML-markup:

<div class="findMva">
    <div class="findMva">
        <input type="text" id="input1" />
        <a class="mva" id="a1" href="#">More info 1...</a>
        <a class="mva" id="a2" href="#">More in开发者_运维知识库fo 2...</a>
    </div>
    <input type="text" id="input2" />
    <div>
        <div>
            <div class="findMva">
                <input type="text" id="input3" />
                <a class="mva" id="a3" href="#">More info 3...</a>
                <a class="mva" id="a4" href="#">More info 4...</a>
                <div class="findMva">
                    <input type="text" id="input4" />
                    <a class="mva" id="a5" href="#">More info 5...</a>
                    <a class="mva" id="a6" href="#">More info 6...</a>
                    <div class="findMva">
                        <input type="text" id="input5" />
                        <a class="mva" id="a7" href="#">More info 7...</a>
                        <a class="mva" id="a8" href="#">More info 8...</a>
                    </div>
                </div>
            </div>
            <a class="mva" id="a9" href="#">More info 9...</a>
            <a class="mva" id="a10" href="#">More info 10...</a>
        </div>
    </div>
</div>

Any suggestions how to write the jQuery selector?


I found a solution:

$(document).ready(function () {
    $("input").keydown(function (e) {
        if (e.which == 13) {
          var o = $(this).closest(".findMva").clone();
            o.find(".findMva").remove();
          var myid = o.find(".mva").first().attr("id");
          $("#" + myid).focus();
        }
    });
});

In this case I'm dependent of an ID-attribute on the A-tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜