简体   繁体   English

带有相同类的多个按钮上的jQuery按钮事件

[英]jquery button event on multiple buttons with the same class

I am having trouble with a button. 我在按按钮时遇到麻烦。

With php I am setting the name of the button, so it is not known beforehand what the name of the button is. 使用php时,我正在设置按钮的名称,因此事先不知道按钮的名称是什么。 The button however have a class, but there are multiple buttons. 但是该按钮有一个类,但是有多个按钮。 When I click on one of the buttons, all buttons fire their event because they all have the same class. 当我单击按钮之一时,所有按钮都会触发其事件,因为它们都具有相同的类。

is there a way that I can make it that only one button fire the event? 有没有一种方法可以使事件触发一个按钮? Here is my code: 这是我的代码:

    <button class="delad" name="<?php print $user; ?>">Delete this ad? </button>

<script>
$(document).ready(function()
{
    count = 0;
    if(count == 0)
    $(".delad").click(function(e)
    {
        e.stopPropagation() 
        v1 =$(this).attr('name');
        sum = {'v1': v1};
        sum = JSON.stringify(sum);

        $.ajax(
                {
                    url: 'del_ad.php',
                     data: 'json='+ sum,
                    success: function(data) 
                    {
                        alert(data)
                    }
                });
                count += 1;

    });

});

</script>

Try the following: 请尝试以下操作:

<button id="dino" class="delad" name="<?php print $user; ?>">Delete this ad? </button>
<script>
$(document).ready(function()
{
count = 0;
if(count == 0)
$("#dino").click(function(e)
{
    e.stopPropagation() 
    v1 =$(this).attr('name');
    sum = {'v1': v1};
    sum = JSON.stringify(sum);

    $.ajax(
            {
                url: 'del_ad.php',
                 data: 'json='+ sum,
                success: function(data) 
                {
                    alert(data)
                }
            });
            count += 1;

});

}); });

You can determine which button that was clicked by using 'this', with 'this' you can access the attribute of the html object. 您可以使用“ this”来确定单击了哪个按钮,使用“ this”可以访问html对象的属性。

$(document).ready(function(){
    $("input[class='my_class']").click(function(){ //get all button with the class of 'myclass'
        alert($(this).attr('name')); //'this' determines which button was clicked
        //ajax call
    });
});

Please see this example :) http://jsfiddle.net/silkherb/o7qpehwg/2/ 请参见以下示例:) http://jsfiddle.net/silkherb/o7qpehwg/2/

I dont know if this would be practical or not but that would depend on how many buttons you have. 我不知道这是否可行,但这取决于您有多少个按钮。

As you can see in the code below you would test to see which button was clicked and then apply the function approriate to it. 正如您在下面的代码中看到的那样,您将进行测试以查看单击了哪个按钮,然后对其应用适当的功能。

I think this will work for you going by the information you gave even though i am assuming the location of the button on the page will be appropriate to a certain function regardless of the name it has. 我认为这将通过您提供的信息为您服务,即使我假设按钮在页面上的位置将适合某个功能,无论其名称如何。

$(".delad").on("click", function(){
    var btnName = $(this).attr("name");
    if($(btnName) == <?php echo $nameOfButton1;?>){

        //do something thats specific 
        //to the button with this name

    }else if($(btnName) == <?php echo $nameOfButton2;?>){

        //do something thats specific 
        //to the button with this name

    }else if($(btnName) == <?php echo $nameOfButton3;?>){

        //do something thats specific 
        //to the button with this name
    }
});

Hope this helps 希望这可以帮助

Well this has happened to me and the problem was the script was called multiple times (in a loop)! 好吧,这发生在我身上,问题是脚本被多次调用(循环)!

Just putting this out there just in case someone has the same problem. 只是把它放在那里,以防万一有人有同样的问题。

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

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