简体   繁体   English

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

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

I'm trying to figure out how to find when my anchor tag is clicked, how to prevent the set of matched elements not to fire.我试图弄清楚如何找到单击我的锚标记的时间,如何防止匹配的元素集不触发。 Here is my Javascript:这是我的 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;
});

Heres the HTML这是 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>

I know that event.target is returning a value based on the index of the element with the matched set.我知道 event.target 根据具有匹配集的元素的索引返回一个值。 How do I tell the other elements to not fire.我如何告诉其他元素不要开火。 Whats happens is, depending on the number of anchor tags with the class of update, will open that many modal windows.发生的情况是,根据更新的 class 的锚标签数量,将打开许多模态 windows。 I just need one, not the whole bunch我只需要一个,而不是全部

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

Got a solution its ugly but it works Give the modal windows equal ids得到了一个丑陋的解决方案,但它可以工作给模式 windows 相等的 ids

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

On click pass the event and get the current target id.单击时传递事件并获取当前目标 ID。 Traverse the DOM tree and find the modal window whose id matches this.遍历 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;
});

Instead of代替

if ($target == $target) {

try尝试

if ($target == this) {

With your approach it would be true for all links which leads to multiple modals opening使用您的方法,所有导致多个模式打开的链接都是正确的

Are you sure that since you are using a class $('.update-form').dialog('open');你确定因为你使用的是 class $('.update-form').dialog('open'); you are not opening more than one dialog because there are more than one element with that class?您没有打开多个对话框,因为该 class 的元素不止一个? i tested your code here and it seems to work:我在这里测试了你的代码,它似乎工作:

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

EDIT - regarding your comment, to stop proapgation of the event you should use stopImmediatePropagation() , to know what part of the DOM triggers your event you should use, as you do event.target编辑-关于您的评论,要停止传播您应该使用stopImmediatePropagation()的事件,以了解 DOM 的哪一部分触发您应该使用的事件,就像您所做的 event.target

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

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