【问题标题】:Jquery replaceWith in a Foreach loopForeach循环中的Jquery replaceWith
【发布时间】:2016-03-26 23:15:25
【问题描述】:

当我执行下面的代码时,所有imgimggid 将被替换,因为它们处于foreach 循环中,但我只想将其应用于单击的那个。任何人都可以帮忙吗?

HTML

<button type="submit" id="getRequest" class="btn btn-info btm-sm " role="button" style="width:100px;height:30px">
<p id="imgg">Add to Cart</p>
 </button>

JS

   $(document).ready(function() {
$(document).on('submit', '#reg-form', function() {

    var data = $(this).find("#post_id").val();
    //var ln = $("#lname").val();

    //var data = 'fname='+fn+'&lname='+ln;

    //  var data = $(this).serialize();
    $.ajax({
        type: 'POST',
        url: '{{url("/ajax")}}',
        data: {
            'name': data,
            '_token': $('input[name=_token]').val()
        },
        success: function(data) {
            $(imgg).replaceWith('<img id=imgg   src=img/ajax.gif> ');

            setTimeout(function() {
                $(imgg).replaceWith('  <p id="imgg">Add to Cart</p> ').hide('blind', {}, 500)
            }, 1300);
            console.log(data);
        },

        error: function(data) {
            alert("You must Login First");
        }
    });
    return false;
});

});

【问题讨论】:

  • 试试 $(this) 选择器
  • 剂量工作:/'无法读取属性'
  • 检查我的新回答者@AchrafKhouadja。我在 anwser 中发布了发生的事情并在 anwser 上发表了评论。
  • 什么是imgg?哪个是点击的?这是什么success 方法?
  • 当按钮被点击时,它会执行一个 post ajax 请求,并用 稍微改变一下

    时间,imgg 是 id

标签: javascript jquery


【解决方案1】:

谢谢大家我找到了解决方案但是你是真正的MVP,这里是

 $(document).ready(function()
{
 $(document).on('submit', '#reg-form', function()
 {
var imgid = $(this).find("#imgg");
var data = $(this).find("#post_id").val();
  //var ln = $("#lname").val();

  //var data = 'fname='+fn+'&lname='+ln;

//  var data = $(this).serialize();
  $.ajax({
  type : 'POST',
  url  : '{{url("/ajax")}}',
 data: {'name':data, '_token': $('input[name=_token]').val()},
 success: function(data) {
    $(imgid).replaceWith('<img id=imgg src=img/ajax.gif> ');
   var thisForm = this;
    setTimeout(function() {
        $(imgg).replaceWith('  <p id=imgg>Add to Cart</p> ').hide('blind', {}, 500)
    }, 1300);
    console.log(data);
},

      error :  function(data)
      {
          alert("You must Login First");
      }
  });
  return false;
 });

});
</script>
<script type="text/javascript">
$.ajaxSetup({
   headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});

【讨论】:

    【解决方案2】:

    而不是使用$(imgg) 使用$(this),并且当你在使用.replaceWith 时给出iamge 的id 时,你也错过了引号:

    success: function(data) {
        $(this).replaceWith('<img id="imgg" src="img/ajax.gif>" ');
       var thisForm = this;
        setTimeout(function() {
            $(thisForm).replaceWith('  <p id="imgg">Add to Cart</p> ').hide('blind', {}, 500)
        }, 1300);
        console.log(data);
    },
    

    原因?

    简单。 this 对象不会改变。它是函数的所有者。在此处查看有关原因的更多信息:Jquery - When to use "this" and when to use "$(this)"?

    编辑2

    现在我必须把它放在上层所以这个:

      $(document).ready(function() {
         $(document).on('submit', '#reg-form', function() {
    
             var vm = this //Upper Level
             var data = $(this).find("#post_id").val();
             $.ajax({
                 type: 'POST',
                 url: '{{url("/ajax")}}',
                 data: {
                     'name': data,
                     '_token': $('input[name=_token]').val()
                 },
                 success: function(data) {
                     $(vm).replaceWith('<img id=imgg   src=img/ajax.gif> ');
    
                     setTimeout(function() {
                         $(vm).replaceWith('  <p id="imgg">Add to Cart</p> ').hide('blind', {}, 500)
                     }, 1300);
                     console.log(data);
                 },
    
                 error: function(data) {
                     alert("You must Login First");
                 }
             });
             return false;
         });
     });
    

    关于这个特殊问题请参考这里:
    Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

    【讨论】:

    • 它抛出这个,无法读取未定义的属性'createDocumentFragment'
    • 是的@AchrafKhouadja 我再次编辑了anwser。你能检查我的代码吗?当您使用 .replaceWith(); 时,您忘记了图像 id 和 src 周围的引号;
    • this inside setTimeout 函数是window 对象。
    • $(vm).replaceWith 这不是选择整个表单还是什么?而你需要在表单中选择 1 个 id 为 imgg 的东西?
    • 嗯这是什么@AchrafKhouadja。您显然必须发布解决方案,以便用户可以看到。
    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 2011-07-12
    • 2013-05-27
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多