简体   繁体   English

jQuery:不能在单个对象上使用“find”方法两次

[英]jQuery: Can't use the “find” method twice on a single object

I'm currently trying to use the jquery "find" method on an object twice but it isn't letting me do it, only the first instance of "find" is working. 我目前正在尝试在对象上使用jquery“find”方法两次,但它不允许我这样做,只有“find”的第一个实例正在工作。 Here is the code that I would like to run... 这是我想要运行的代码......

$("#phone_numbers > p:first-child").clone(true).insertBefore("#phone_numbers > span:last-child").find('.phone_type, .phone_number, .user_phones_id').val('').find('.delete_phone').remove();

The above code works just fine except for the last "find" method, it isn't removing the elements with a ".delete_phone" class. 上面的代码工作正常,除了最后一个“查找”方法,它不是删除带有“.delete_phone”类的元素。

However, if I change the code to look like this... 但是,如果我将代码更改为这样......

$("#phone_numbers > p:first-child").clone(true).insertBefore("#phone_numbers > span:last-child").find('.delete_phone').remove();

It does remove the elements with a ".delete_phone" class. 它确实删除了带有“.delete_phone”类的元素。 I assume this is because I can't use the "find" method twice in a row, but I'm not sure. 我认为这是因为我不能连续两次使用“find”方法,但我不确定。

Does anyone know what's going on or if there is a way around this problem? 有谁知道发生了什么,或者是否有办法解决这个问题?

Thanks! 谢谢!

You need a .end() to hop back in the chain (so you're not looking inside the previous .find() results), like this: 你需要一个.end()跳回到链中(所以你不要查看之前的.find()结果),如下所示:

$("#phone_numbers > p:first-child").clone(true).insertBefore("#phone_numbers > span:last-child").find('.phone_type, .phone_number, .user_phones_id').val('').end().find('.delete_phone').remove();

Broken down view: 细分视图:

$("#phone_numbers > p:first-child").clone(true)
  .insertBefore("#phone_numbers > span:last-child")
  .find('.phone_type, .phone_number, .user_phones_id') //look in the cloned <p>
  .val('')                                             //empty those inputs
  .end()                                               //hope back to the <p>
  .find('.delete_phone')                               //look in the clone <p>
  .remove();                                           //remove those elements

It looks like you are trying to cram a lot of requests into one line when you don't really need to. 看起来你正在尝试将很多请求塞进一行,当你真的不需要时。 I'm not exactly sure what you are aiming to do, but I would manage it more like this. 我不确定你的目标是什么,但我会更像这样管理它。

Instead let's break it down into three simple requests: 相反,让我们将其分解为三个简单的请求:

var numbers = $("#phone_numbers > p:first-child");//grab the data you want
numbers.insertBefore("#phone_numbers > span:last-child");//insert it where you want
$("#phone_numbers .delete_phone').remove();//remove numbers with class 'delete_phone'

Not sure what you are trying to do for the val() part as you are not storing the value in a variable, neither are you changing the value. 不确定你要为val()部分做什么,因为你没有将值存储在变量中,也没有更改值。

If you need more help message back. 如果您需要更多帮助消息。

W. W.

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

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