简体   繁体   English

有一个Jquery选择器可以工作,但在$ .post中不起作用

[英]Have a Jquery selector that works, but does not work in $.post

I have this Jquery line which finds the cell I would like to input data into, This works when I use it outside of my $.post function (below), but not inside my $.post 我有这个Jquery行,它找到了我想要输入数据的单元格,当我在我的$ .post函数(下面)之外使用它时,这是有效的,但不在我的$ .post中

$(this).parent().parent().find(".total_item_price").html("new text");

But when I use the above line in the below example, It does not work. 但是当我在下面的示例中使用上面的行时,它不起作用。 It doesn't update the text in the required field. 它不会更新必填字段中的文本。

$.post(url, { "params": value}, function(data) {

          // This is where I am having a problem
          // also tried .text and .val
          $(this).parent().parent().find(".total_item_price").html(data.total_price_item);

          alert(data.total_price_item); // This outputs my data
       }, "JSON"); 

I basically want to put my returned (data) into the cell of the table. 我基本上想把我的返回(数据)放到表格的单元格中。 I don't think that my table structure is the issue, but I can post it if needed. 我不认为我的表结构是问题,但如果需要我可以发布它。

I should add, that I have to find the parents and field, because each post function will output into a different cell, so its relative to where the user clicked. 我应该补充一点,我必须找到父和字段,因为每个post函数都会输出到不同的单元格,因此它相对于用户点击的位置。

Whats going wrong in this code? 这段代码出了什么问题?

Because this inside your load callback is not equal to this outside of your load callback. 因为this您的负载回调中不等于this之外您的负载回调。 They have different scopes. 它们有不同的范围。

Before you call the post, cache the this from the other scope into a variable: 在调用帖子之前,将this从另一个范围缓存到变量中:

var scope = this;

And then in your callback you can reference the other this as scope : 然后在你的回调可以参考其他thisscope

$(scope).parent().parent().find(".total_item_price").html(data.total_price_item);

In $.post this will not point to the same element which you are thinking. $.post this不会指向您正在考虑的相同元素。 Please use the actual selector to make it work. 请使用实际的选择器使其工作。

因为你的函数中的“this”是对函数的引用,而不是对节点的引用!

($this) is not what you think it is! ($this)不是你想的那样! this is always the owner of the function being executed, which in the $.post is the callback function. this 始终是正在执行的函数的所有者, $.post中的函数是回调函数。

you can do it like this, 你可以这样做,

  var a= $(this);
  $.post(url, { "params": value}, function(data) {

          // This is where I am having a problem
          // also tried .text and .val
          a.parent().parent().find(".total_item_price").html(data.total_price_item);

          alert(data.total_price_item); // This outputs my data
       }, "JSON"); 

$(this) will not work there. $(this)不会在那里工作。 As it is in the scope of the callback function, not the click event. 因为它在回调函数的范围内,而不是click事件。

Assign $(this) to a variable outside of the post (inside the click event handler) and then use that inside the post callback. $(this)分配给帖子之外的变量(在click事件处理程序内),然后在post回调中使用它。

  var that = $(this);
  $.post(url, { "params": value}, function(data) {
      that.parent().parent().find(".total_item_price").html(data.total_price_item);
      alert(data.total_price_item); // This outputs my data
  }, "JSON"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM