使用 Jquery 查找父 div 的 id

我有一些这样的 html:

<div id="1">
<p>
Volume = <input type="text" />
<button rel="3.93e-6" class="1" type="button">Check answer</button>
</p>
<div></div>
</div>

还有这样的 JS:

$("button").click(function () {
var buttonNo = $(this).attr('class');
var correct = Number($(this).attr('rel'));


validate (Number($("#"+buttonNo+" input").val()),correct);
$("#"+buttonNo+" div").html(feedback);
});

我真正想要的是,如果我不必在按钮上有 class = “1”(我知道数字类是无效的,但这是一个 WIP!)这样我就可以根据父 div 的 id 来确定 ButtonNo。在现实生活中,有多个部分看起来像这样。

  1. 我如何找到子女的 ID 按钮。

  2. 在按钮代码中存储答案的更语义化的方法是什么。我想使这一点尽可能简单,非程序员复制和粘贴,而不打破东西!

284475 次浏览

To get the id of the parent div:

$(buttonSelector).parents('div:eq(0)').attr('id');

Also, you can refactor your code quite a bit:

$('button').click( function() {
var correct = Number($(this).attr('rel'));
validate(Number($(this).siblings('input').val()), correct);
$(this).parents('div:eq(0)').html(feedback);
});

Now there is no need for a button-class

explanation
eq(0), means that you will select one element from the jQuery object, in this case element 0, thus the first element. http://docs.jquery.com/Selectors/eq#index
$(selector).siblings(siblingsSelector) will select all siblings (elements with the same parent) that match the siblingsSelector http://docs.jquery.com/Traversing/siblings#expr
$(selector).parents(parentsSelector) will select all parents of the elements matched by selector that match the parent selector. http://docs.jquery.com/Traversing/parents#expr
Thus: $(selector).parents('div:eq(0)'); will match the first parent div of the elements matched by selector.

You should have a look at the jQuery docs, particularly selectors and traversing:

Try this:

$("button").click(function () {
$(this).parents("div:first").html(...);
});

1.

$(this).parent().attr("id");

2.

There must be a large number of ways! One could be to hide an element that contains the answer, e.g.

<div>
Volume = <input type="text" />
<button type="button">Check answer</button>
<span style="display: hidden">3.93e-6&lt;/span>
<div></div>
</div>

And then have similar jQuery code to the above to grab that:

$("button").click(function ()
{
var correct = Number($(this).parent().children("span").text());
validate ($(this).siblings("input").val(),correct);
$(this).siblings("div").html(feedback);
});

bear in mind that if you put the answer in client code then they can see it :) The best way to do this is to validate it server-side, but for an app with limited scope this may not be a problem.

JQUery has a .parents() method for moving up the DOM tree you can start there.

If you're interested in doing this a more semantic way I don't think using the REL attribute on a button is the best way to semantically define "this is the answer" in your code. I'd recommend something along these lines:

<p id="question1">
<label for="input1">Volume =</label>
<input type="text" name="userInput1" id="userInput1" />
<button type="button">Check answer</button>
<input type="hidden" id="answer1" name="answer1" value="3.93e-6" />
</p>

and

$("button").click(function () {
var correctAnswer = $(this).parent().siblings("input[type=hidden]").val();
var userAnswer = $(this).parent().siblings("input[type=text]").val();
validate(userAnswer, correctAnswer);
$("#messages").html(feedback);
});

Not quite sure how your validate and feedback are working, but you get the idea.

You could use event delegation on the parent div. Or use the closest method to find the parent of the button.

The easiest of the two is probably the closest.

var id = $("button").closest("div").prop("id");

This can be easily done by doing:

$(this).closest('table').attr('id');

You attach this to any object inside a table and it will return you the id of that table.

http://jsfiddle.net/qVGwh/6/ Check this

   $("#MadonwebTest").click(function () {
var id = $("#MadonwebTest").closest("div").attr("id");
alert(id);
});
$(this).parents('div').attr('id');

find() and closest() seems slightly slower than:

$(this).parent().attr("id");