定位特定元素的jQuery

用户名

我在网上搜索了此内容,但是找不到解决方案,只是我想button用ajax响应替换element内部的内容我面临的唯一问题是,整个div都在改变,而不是我单击的那个。如果我将类更改为id,则只有第一个不会更改我想要的特定按钮。那么,如何替换特定按钮的内容?

PHP代码:

while($row = $result->fetch_assoc()){// there are four rows so I have 4 buttons.
echo "<button class=btn btn-primary test' value='."$row['id']".'>."$row['voteup']".</button>"; 
 }

JavaScript代码:

$(function() {
 $('.test').on('click', function() { // attach the click to the button
var test_val = $(this).val(); // Grab the value from the button
$.ajax({
    type: "GET",
    url: "test.php", // Move ?tar=vrate to the data array.
    data: {tar: 'vrate', test: test_val},
    cache: false,
    success: function(response)
    {  
        $(".test").fadeIn('slow').html(response);//this is replacing all 4 buttons not just one, if I change the class to id, only the first one is being replaced with the response. I also tried $( .test", this) but no luck.

    }
});
 });
});
拉贾普布(Rajabrabhu Aravindasamy)

尝试使用$(this)此处参考来实现您想要的,

$('.test').on('click', function() { // attach the click to the button
   var test_val = $(this).val(); // Grab the value from the button
   var $this = $(this);
     $.ajax({
       type: "GET",
       url: "test.php", // Move ?tar=vrate to the data array.
       data: {tar: 'vrate', test: test_val},
       cache: false,
       success: function(response)
         {  
           $this.fadeIn('slow').html(response);
         }
     });
 });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从ConcurrentLinkedDeque中的特定位置获取元素

来自分类Dev

使用jquery或简单js将元素拖动到特定位置

来自分类Dev

使用jQuery定位和移动子元素

来自分类Dev

jQuery-动画元素定位

来自分类Dev

CSS-定位特定元素

来自分类Dev

在特定位置停止固定元素滚动?

来自分类Dev

在特定位置增加arrayList的元素

来自分类Dev

在jQuery中定位特定表

来自分类Dev

jQuery点击事件定位到多个元素

来自分类Dev

使用jQuery定位特定的div

来自分类Dev

单击jQuery以定位组中的特定元素

来自分类Dev

jQuery选择器,以按元素及其子元素定位元素的点击,只要它也没有特定的类

来自分类Dev

粘性元素在特定位置更改颜色

来自分类Dev

jQuery:使用prev()定位元素

来自分类Dev

jQuery在分离文件中定位SVG元素

来自分类Dev

从ConcurrentLinkedDeque中的特定位置获取元素

来自分类Dev

使用jquery或简单的js将元素拖动到特定位置

来自分类Dev

在特定位置停止固定元素滚动?

来自分类Dev

在特定位置增加arrayList的元素

来自分类Dev

jQuery toggleClass定位正确的元素

来自分类Dev

尝试定位父元素子JQuery

来自分类Dev

XSLT在特定位置添加元素

来自分类Dev

如何使用jquery定位此元素?

来自分类Dev

jQuery:不定位元素

来自分类Dev

在我的Jquery按钮元素之外定位div

来自分类Dev

使用CSS / JS定位特定元素之后的目标元素

来自分类Dev

使用 append 定位 jQuery 元素

来自分类Dev

匹配列表特定位置的元素

来自分类Dev

XPATH - 定位特定的子元素