【问题标题】:Post to one specific class not all classes with the same name [duplicate]发布到一个特定的班级并非所有具有相同名称的班级[重复]
【发布时间】:2011-12-17 16:45:43
【问题描述】:

可能重复:
Finding class where button is clicked

<script>
          var num = 1;
function ajax_post(){ 
$.ajax('javas.php', {
success: function(response) {
      $(".status").html(response);
}, 
data: "num=" + (++num)
});
}

function ajax_posta(){
$.ajax('javas.php', {
success: function(response) {
      $(".status").html(response);
}, 
data: "num=" + (--num)
});
}

$(document).ready(function() {
$('.eventer > .button').click(function () {
    ajax_post();
});
alert("lol");
});


</script>

这是发布到具有名称状态的所有类,但是我希望它只发布到单击提交按钮的类,因为类是在 while 循环中生成的,所以有许多重复的类。 这是在 PHP while 循环中生成的 HTML:

echo " <div class = 'eventer'> $timeposted <br>$eventlist <button name='myBtn'     type='button'  onClick='javascript:ajax_post();'>Inc</button>
                          <button name='lol' type='button' value='dec'         onClick='javascript:ajax_posta();'></button></div>
<div class = 'status'>sigh</div>";

回显“
”;

【问题讨论】:

  • 你能展示一下 HTML 部分吗?
  • 我们需要查看您的 html

标签: jquery ajax class


【解决方案1】:

将您的 ajax_post 方法更改为

function ajax_post(obj){
    $.ajax('javas.php', {   success: function(response) { $(obj).closest('.status').html(response);   },    
    data: "num=" + (++num)   });   }   

将此方法称为 ajax_post(这个);

【讨论】:

  • @foshoeiyyy 在按钮点击事件 $('.eventer > .button').click(function () { ajax_post(this); });
  • 查找带有“.status”类的 div 的问题。我没有查看您最近更新的 HTML
  • @foshoeiyyy $(obj).parent().next('.status').html(response)
  • 还是什么都没有,什么都不显示
猜你喜欢
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多