如何使jquery定位单击按钮而不是第一个实例 [英] How do I make jquery target the button being clicked instead of the first instance

查看:82
本文介绍了如何使jquery定位单击按钮而不是第一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JQuery函数。最近需求发生了变化,现在我们将会有很多需求而不是单个按钮。尝试之后出现问题,无论您点击什么按钮,JQuery都会从第一个按钮检索数据。需要修改它才能从被点击的一个实例中获取它。



(函数($){
$ (。content-button)。click(function(e){
e.preventDefault();

var $ contentWrapper = $(#content-wrapper);
var postId = $ contentWrapper.data(id);

$ .ajax({
url:/ direct.php,
type:POST ,
数据:{
post_id:postId
},
})
.done(函数(数据){
$ contentWrapper.html( '');
$ contentWrapper.append(data);
});
});
$(。content-button)。mouseup(function(e) {
if(e.which == 1){
$(。content-button)。hide();
}
});
} )(jQuery);





我尝试过:



我想我必须使用(这个)但我尝试失败了,我不太了解JQuery。

解决方案

){


(。content-button)。click(function(e){
e.preventDefault();

var


contentWrapper =


Hi, i have a JQuery function. Recently the needs changed and now instead of a single button we will have many. After trying it out there is a problem, no matter what button you click JQuery retrieves the data from the first button. It needs to be modified to get it from the one instance that is being clicked.

(function($) {
  $(".content-button").click(function(e) {
    e.preventDefault();

    var $contentWrapper = $("#content-wrapper");
	var postId = $contentWrapper.data("id");

    $.ajax({
        url: "/direct.php",
        type: "POST",
        data: {
          "post_id": postId   	  
        },
      })
      .done(function(data) {
      	$contentWrapper.html('');
        $contentWrapper.append(data);
      });
  });
  $(".content-button").mouseup(function(e) {
	  if (e.which == 1) {
      $(".content-button").hide();
    }
  });
})(jQuery);



What I have tried:

I think i have to use (this) but all i tried failed, i don't understand JQuery very well.

解决方案

) {


(".content-button").click(function(e) { e.preventDefault(); var


contentWrapper =


这篇关于如何使jquery定位单击按钮而不是第一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆