2017-03-08 95 views
-3

我想发送一个函数中的参数,但它在一个循环中,所以当我选择相同的函数下一次它首次发送给我以前的值然后发送给我的价值,我想要的这导致函数是空的,而不是采取任何价值让我告诉你我的代码。如何在函数参数传递给函数时清空它?

$(window).load(function(e) { 
    loadmore(); 
    select_likes(); 
    select_share(); 
    // get_recieve_friend_requests(); 
    // get_sent_friend_requests(); 
}); 

function loadmore() { 
    var lastID = $('.load-more').attr('lastID'); 
    // alert(lastID); 

    jQuery.ajax({ 
    type: 'POST', 
    url: '<?php echo base_url("user/get_all_post"); ?>', 
    data: { 
     id: lastID 
    }, 
    dataType: 'json', 
    beforeSend: function(data) { 
     $('.load-more').show(); 
    }, 
    success: function(data) { 
     var ParsedObject = JSON.stringify(data); 
     var json = $.parseJSON(ParsedObject); 

     if (json == "") { 
     $("#bottom").append('<div class="btn btn-default col-md-6" >' + 'No More Results' + '</div>'); 
     $("#Load_more_data").hide(); 
     } else { 
     $postID = json[json.length - 1].id; 
     $('.load-more').attr('lastID', $postID); 
     $.each(json, function(key, data) { 
      var post_id = data.id; 
      var post_status = data.status; 
      var status_image = data.status_image; 
      var multimage = data.multimage; 

      if (!post_status == "" && !status_image == "") { 
      alert(post_id); 
      $("#status_data").append('<div class="media-body"><div class="input-group"><form action="" id="form_content_multimage"><textarea name="textdata" id="content_comment_multimage" cols="25" rows="1" class="form-control message" placeholder="Whats on your mind ?"></textarea><button type="submit" id="comment_button_multimage" onclick="comment_here_multimage(' + post_id + ');" >Comment</button><?php echo form_close();?></div></div></li></ul></div></div>'); 
      } 
     }); 
     } 
    } 
    }); 
} 

function comment_here_multimage(post_id) { 
    $(document).on('click', '#comment_button_multimage', function(e) { 
    // this will prevent form and reload page on submit. 
    e.preventDefault(); 
    var post_id_multimage = $('#post_id_multimage').val(); 
    // here you will get Post ID 
    alert(post_id_multimage); 
    var Post_id = post_id; 
    alert(post_id); 

    if (post_id == post_id_multimage) { 
     var User_id = $('.id_data').attr('value'); 
     var textdata = $('#content_comment_multimage').val(); 
     alert(textdata); 
     alert(Post_id); 
     $.ajax({ 
     type: 'POST', 
     url: '<?php echo base_url("user/post_comment"); ?>', 
     data: { 
      Post_id: Post_id, 
      User_id: User_id, 
      textdata: textdata 
     }, 
     dataType: 'json', 
     success: function(data) { 
      console.log(data); 
      alert('you have like this'); 
      jQuery('#form_content_multimage')[0].reset(); 
      Post_id = ""; 
     } 
     }); 
    } else { 
     return false; 
    } 
    }); 
} 

的POST_ID正在通过comment_here_multimage的onclick事件,但whenver我第一次相同的ID后,点击它被传递再次第一则下一个ID通过。我可以在完成后清空post_id值。

看看这些图像,并告诉我,如果有什么你不明白。

[![first time comment][1]][1]  
[![second time comment][2]][2] 
[![second time comment][3]][3] 


    [1]: https://i.stack.imgur.com/b36o4.png 
    [2]: https://i.stack.imgur.com/ahg3W.png 
    [3]: https://i.stack.imgur.com/taHAS.png 
+1

你能找出问题好?我没有设法理解你想要做什么以避免问题的产生。 – rafaelxy

+0

在你的html中执行post函数后有不同的append div值吗? – bxN5

+0

请看编辑过的@rafaelxy –

回答

2

您的问题未在代码中分离。但根据我的理解。

创建动态html时,您正在绑定“comment_here_multimage”函数的按钮点击事件。 加载上下文后,用户单击该按钮,再次绑定同一按钮上的另一个功能,最终将该功能添加到事件堆栈中。

如果用户第一次点击按钮什么都不会发生,那么就没有任何操作。第一次它会注册一个处理程序。 如果用户第二次点击,它会触发第一次点击时附加的处理程序,导致旧的postid被提供给它。

我认为你的问题是传递参数。您可以将其设置为自定义参数,稍后通过点击处理程序获取。或者你可以像下面那样改变你的处理器。

你可以改变你这样的代码

onclick="comment_here_multimage(this,' + post_id + ');" 

    function comment_here_multimage(e,post_id) { 

    // this will prevent form and reload page on submit. 
    e.preventDefault(); 
    var post_id_multimage = $('#post_id_multimage').val(); 
    // here you will get Post ID 
    alert(post_id_multimage); 
    var Post_id = post_id; 
    alert(post_id); 

    if (post_id == post_id_multimage) { 
     var User_id = $('.id_data').attr('value'); 
     var textdata = $('#content_comment_multimage').val(); 
     alert(textdata); 
     alert(Post_id); 
     $.ajax({ 
     type: 'POST', 
     url: '<?php echo base_url("user/post_comment"); ?>', 
     data: { 
      Post_id: Post_id, 
      User_id: User_id, 
      textdata: textdata 
     }, 
     dataType: 'json', 
     success: function(data) { 
      console.log(data); 
      alert('you have like this'); 
      jQuery('#form_content_multimage')[0].reset(); 
      Post_id = ""; 
     } 
     }); 
    } else { 
     return false; 
    } 
    ; 
} 
+0

它的唯一重新加载我的网页,并没有工作@Mudassar Hasnain –

+0

onclick =“comment_here_multimage(this,'+ post_id +');”你有没有在你的附加div代码中更改此行 –

+0

我需要在我的数据库中首先输入值,因为我需要选择器来选择值但它不工作@Mudassar Hasnain –