繁体   English   中英

在一组匹配元素中的一个元素上触发单击事件,而不是在匹配元素上触发

[英]Fire click event on one element in a set of matched elements and not on matched elements

我试图弄清楚如何找到单击我的锚标记的时间,如何防止匹配的元素集不触发。 这是我的 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 根据具有匹配集的元素的索引返回一个值。 我如何告诉其他元素不要开火。 发生的情况是,根据更新的 class 的锚标签数量,将打开许多模态 windows。 我只需要一个,而不是全部

//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');
    }
});

得到了一个丑陋的解决方案,但它可以工作给模式 windows 相等的 ids

$updateForm.each(function(index) {
    $(this).attr('id', index);
});

单击时传递事件并获取当前目标 ID。 遍历 DOM 树并找到 id 与此匹配的模态 window。

//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;
});

代替

if ($target == $target) {

尝试

if ($target == this) {

使用您的方法,所有导致多个模式打开的链接都是正确的

你确定因为你使用的是 class $('.update-form').dialog('open'); 您没有打开多个对话框,因为该 class 的元素不止一个? 我在这里测试了你的代码,它似乎工作:

http://jsfiddle.net/aMsbT/1/

编辑-关于您的评论,要停止传播您应该使用stopImmediatePropagation()的事件,以了解 DOM 的哪一部分触发您应该使用的事件,就像您所做的 event.target

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM