如何引用函数的根元素?

huangapple go评论93阅读模式
英文:

Jquery - how to refer to the root element of the function?

问题

这里我正在将jQuery的自动完成功能应用于我的页面上所有具有main-item类的文本框。

问题出在这一行:var search = ($this).val().toLowerCase();。我试图获取文本框的文本,但在这里$this只是指代了从ajax调用返回的数据。我该如何从调用此函数的$(".main-item")元素中获取文本?

  1. $(function () {
  2. $(".main-item").autocomplete({
  3. source: function( request, response ) {
  4. $.ajax({
  5. url: "https://api.test.com",
  6. type: "GET",
  7. dataType: "xml",
  8. success: function (data) {
  9. var matches = [];
  10. // 在这里获取文本框的文本
  11. var search = $(this).val().toLowerCase();
  12. $(data).find("string").each(function () {
  13. var item = $(this).text();
  14. if (item.toLowerCase().indexOf(search) > -1) {
  15. matches.push(item);
  16. }
  17. });
  18. response(matches);
  19. },
  20. error: function (req, err) {
  21. console.log(req)
  22. }
  23. });
  24. },
  25. minLength: 2,
  26. select: function( event, ui ) {
  27. $(".main-item").val(ui.item.value);
  28. $("span.input-group-btn > input").click();
  29. }
  30. });
  31. });
英文:

Here I'm applying jquery's autocomplete function to all the textboxes on my page with the main-item class.

The problem is on the line var search = ($this).val().toLowerCase(); I'm trying to get the text of the textbox, but here $this just refers to the data returned from the ajax call. How can I get the text from $( ".main-item" ) element that invoked this function instead?

  1. $(function () {
  2. $( ".main-item" ).autocomplete({
  3. source: function( request, response ) {
  4. $.ajax({
  5. url: "https://api.test.com",
  6. type: "GET",
  7. dataType: "xml",
  8. success: function (data) {
  9. var matches = [];
  10. var search = ($this).val().toLowerCase();
  11. $(data).find("string").each(function () {
  12. var item = $(this).text();
  13. if (item.toLowerCase().indexOf(search) > -1) {
  14. matches.push(item);
  15. }
  16. });
  17. response(matches);
  18. },
  19. error: function (req, err) {
  20. console.log(req)
  21. }
  22. });
  23. },
  24. minLength: 2,
  25. select: function( event, ui ) {
  26. $(".main-item").val(ui.item.value);
  27. $("span.input-group-btn > input").click();
  28. }
  29. });
  30. });

答案1

得分: 1

你应该使用jQuery::each()逐个处理每个文本框,并将文本框保存到变量以供以后使用。
请不要使用var,而是使用现代的const/let代替。

  1. $(function () {
  2. $(".main-item").each(function(_, textbox){
  3. const $textbox = $(textbox);
  4. $textbox.autocomplete({
  5. source: function( request, response ) {
  6. $.ajax({
  7. url: "https://api.test.com",
  8. type: "GET",
  9. dataType: "xml",
  10. success: function (data) {
  11. const matches = [];
  12. const search = $textbox.val().toLowerCase();
  13. $(data).find("string").each(function () {
  14. const item = $(this).text();
  15. if (item.toLowerCase().indexOf(search) > -1) {
  16. matches.push(item);
  17. }
  18. });
  19. response(matches);
  20. },
  21. error: function (req, err) {
  22. console.log(req)
  23. }
  24. });
  25. },
  26. minLength: 2,
  27. select: function( event, ui ) {
  28. $textbox.val(ui.item.value);
  29. // 这里可能有一个bug
  30. $("span.input-group-btn > input").click();
  31. }
  32. });
  33. });
  34. });
英文:

You should handle each textbox individually with jQuery::each() and save a texbox to a variable for later use.
And please for god's sake don't use var, use modern const/let instead...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(function () {
  2. $( &quot;.main-item&quot; ).each(function(_, textbox){
  3. const $textbox = $(textbox);
  4. $texbox.autocomplete({
  5. source: function( request, response ) {
  6. $.ajax({
  7. url: &quot;https://api.test.com&quot;,
  8. type: &quot;GET&quot;,
  9. dataType: &quot;xml&quot;,
  10. success: function (data) {
  11. var matches = [];
  12. var search = $textbox.val().toLowerCase();
  13. $(data).find(&quot;string&quot;).each(function () {
  14. var item = $(this).text();
  15. if (item.toLowerCase().indexOf(search) &gt; -1) {
  16. matches.push(item);
  17. }
  18. });
  19. response(matches);
  20. },
  21. error: function (req, err) {
  22. console.log(req)
  23. }
  24. });
  25. },
  26. minLength: 2,
  27. select: function( event, ui ) {
  28. $textbox.val(ui.item.value);
  29. // you have a bug here maybe
  30. $(&quot;span.input-group-btn &gt; input&quot;).click();
  31. }
  32. });
  33. });
  34. });

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月22日 06:45:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定