【发布时间】:2011-07-06 15:40:45
【问题描述】:
我试图弄清楚如何找到单击我的锚标记的时间,如何防止匹配的元素集不触发。这是我的 Javascript:
//open the dialog box
$('.update').click(function (event) {
var $target = event.target;
if ($target == $target) {
$('.update-form').dialog('open');
console.log('yep');
} else {
console.log('nope');
}
return false;
});
这是 HTML
<tbody>
<tr>
<th>Designer</th>
<th>Style</th>
<th>Color</th>
<th>Size</th>
<th>Detials</th>
<th>Notes</th>
</tr>
<tr style="background-color: rgb(201, 201, 201); color: rgb(255, 255, 255); ">
<td>JASZ COUTURE</td>
<td>4210</td>
<td>GOLD</td>
<td>S</td>
<td> STRAPPY STETCH COCKTAIL</td>
<td></td>
<td><a href="http://localhost:8888/lexsreg/index.php/#" class="update">UPDATE</a></td>
</tr>
<tr>
<td>test</td>
<td>4as451</td>
<td>test</td>
<td>test</td>
<td>tes</td>
<td>tes</td>
<td><a href="http://localhost:8888/lexsreg/index.php/#" class="update">UPDATE</a></td>
</tr>
</tbody>
我知道 event.target 正在返回一个基于具有匹配集的元素的索引的值。我如何告诉其他元素不要开火。发生的情况是,根据更新类别的锚标记的数量,将打开那么多模式窗口。我只需要一个,而不是全部
//set the functions of the dialog box
$('.update-form').dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
draggable: false,
resizable: false,
buttons: {
'Update': function() {
//json will happen here
},
'Cancel': function() {
$(this).dialog('close');
}
},
close: function() {
allFields.val('').removeClass('ui-state-error');
}
});
找到了一个丑陋但有效的解决方案 给模态窗口相同的 ids
$updateForm.each(function(index) {
$(this).attr('id', index);
});
单击时传递事件并获取当前目标 ID。遍历 DOM 树,找到 id 与 this 匹配的模态窗口。
//open the dialog box for the rows
$($btns).click(function(event) {
var target = event.currentTarget.id,
form = $(this)
.parent()
.parent()
.parent()
.parent()
.parent()
.parent()
.siblings()
.children()
.filter('#'+target);
$(form).dialog('open');
return false;
});
【问题讨论】:
-
我们能看到
.update-form的代码吗?如果您在每个tr中包含该类的一个元素,则需要使用一些jQuery 选择器来获取最接近被单击的a的实例,但我们不知道这个表单在哪里。从表面上看,这只是一个实例,但我无法理解您所说的“如何防止匹配的元素集不被触发”是什么意思。你能澄清一下吗? -
@JamWaffles 是的,我也有点困惑。 @Andre Dublin 请让我知道我是否错过了答案,以便我可以编辑或删除它,如果它不正确。
标签: javascript jquery jquery-selectors jquery-events