0
我这是在有几个button.enter-match
元素长表的末尾运行下面的代码:如何获得第一个jQuery对象的父级?
$("button.enter-match")
.button()
.on('click', function() {
$("form.enter-match").dialog({
modal: true,
height: 'auto',
width: 200,
close: function() {
$("form.enter-match input[name='code']").val('');
},
open: function() {
$("form.enter-match input[name='code']").val('').focus();
},
buttons: {
Set: function() {
pid = $(this).parents("[data-pid]").data('pid');
if ($("form.enter-match input[name='code']").val() == '') {
alert('Code empty!');
return false;
}
$.post(
request_url,
{
'action': 'set_match',
'pid': pid,
'code': $("form.enter-match input[name='code']").val()
},
function (data) {
error_handler(data);
if (data['error'] == null) {
$("tr#pid-" + pid + " td.code:first div").html('<i>' + $("form.enter-match input[name='code']").val() + '</i>');
$("form.enter-match").dialog('close');
}
},
'json'
);
}
}
});
});
线pid = $(this).parents("[data-pid]").data('pid');
因为form#enter_match
在的最顶端创建没有得到pid
数据值根据需要在代码中重新使用文档。因此,它不会有一个具有[data-pid]
属性的父项,但是,button.enter-match
将会。我如何从代码的$("form.enter-match").dialog()
部分中获取[data-pid]
中特定button.enter-match
的值?
ID是为了在文档中唯一,如果你打算复制一段HTML,你不应该有任何ID。您的选择器将无法正常工作。你应该给这些元素描述类。 – 2013-04-25 21:11:54
@PaoloBergantino - 您应该是唯一的ID是完全正确的,但指定标记名和ID _will_的选择器仍然有效。 (一个只使用ID的选择器只能找到一个,就像'getElementById()'只能找到一个一样。) – nnnnnn 2013-04-25 21:13:33
@nnnnnn - 不要防守不好的练习/无效的HTML,它不应该完成=> http:/ /stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-types – PlantTheIdea 2013-04-25 21:14:59