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

时间:2011-07-06 15:40:46

标签: javascript jquery javascript-events jquery-selectors

我正在试图弄清楚如何在点击我的锚标签时找到,如何防止匹配元素集合不被触发。这是我的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与此匹配的模态窗口。

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

2 个答案:

答案 0 :(得分:1)

而不是

if ($target == $target) {

if ($target == this) {

根据您的方法,对于导致多个模态打开的所有链接都是如此

答案 1 :(得分:0)

你确定因为你正在使用类$('。update-form')。dialog('open');你没有打开多个对话框,因为该类有多个元素?我在这里测试了你的代码,它似乎有效:

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

编辑 - 关于你的评论,要停止对你应该使用stopImmediatePropagation()的事件进行分类,要知道你应该使用哪个DOM部分来触发你应该使用的事件,就像你做的那样。目标

相关问题