【问题标题】:JQuery each loop concat this.text with stringJQuery 每个循环将 this.text 与字符串连接起来
【发布时间】:2013-06-02 10:26:33
【问题描述】:

我有一个应该发送私人消息的每个循环:

[...].each(function () { $.get('board.php?func=list&id=' + (this.text) + '&send=Please read the board rules'); });

当我执行 'alert(this.text);'在每个循环中,一切正常。但是当我将它与获取请求的字符串连接时,它不起作用。 我错过了什么吗?

【问题讨论】:

  • this 到底是什么,告诉我们它的使用范围?如果是元素,应该是$(this).text()
  • 尝试单独定义 url 并记录它以查看错误所在,例如[...].each(function () { var url = 'board.php?func=list&id=' + (this.text) + '&send=Please read the board rules'; console.log(url); $.get(url); });
  • @adeneo:我正在使用的数据来自另一个获取请求。我现在在回调函数中工作:$(data from get request).find('#boardmembers').children('members').each[...]。其余的在上面;)
  • 然后你正在处理元素,并且在 each() this 内部将是一个本机 DOM 元素,它没有 text 属性,你应该使用 $(this).text() 来获取文本,你试过了吗?

标签: jquery get concatenation each


【解决方案1】:

也许用 $.ajax 写得更详细一点,可以更清楚为什么需要使用$(this).text(),而不是this.text

$(data from get request).find('#boardmembers')
                        .children('members')
                        .each(function() { 
                             $.ajax({
                                 type: 'GET',
                                 url : 'board.php',
                                 data: {
                                        func:'list',
                                        id  : $(this).text(),
                                        send: 'Please read the board rules'
                                 }
                             });
                        });

【讨论】:

  • 以后我只会使用这种风格。帮助很大。谢谢大佬!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
相关资源
最近更新 更多