jquery - 稍后使用 $(this) 不可能,有什么替代方法?

标签 jquery sharepoint this

我剪下了这段小代码:

$(document).ready(function () {
$('td').each(function(index) {
    thisValue = "";                                     //Variable leeren
                                                    //alert(index + ': ' + $(this).attr("id"));
    currentField = $(this).attr("tid");
                                                    //alert (currentField);
    currentID = $(this).attr("ftn");
                                                    //alert (currentID);
    $SP().list(TableName).get({
        fields: currentField,
        where:"ID = "+currentID
    },function getData(row) {
        console.log (row[0].getAttribute(currentField) *1);
        thisValue = (row[0].getAttribute(currentField) *1);
        $(this).text(thisValue);                    // Wert der Listenzelle in die HTML Tabelle schreiben
    })

});

它工作正常,如果我执行 console.log,每个搜索到的单元格元素的值将显示在控制台中。但实际上我无法将这些值带入页面,我认为这是因为 $(this) 不再“连接”到我认为的 html 页面中的表格单元格。 这是我的 HTML 构造:

<div class="uk-panel uk-panel-box uk-panel-box-muted">
                                    <h4>Product 1</h4>
                                    <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
                                        <tr><th>Service Requests:</th>  <td tid="product1" ftn="1" id="sr_acvl">NULL</td></tr>
                                        <tr><th>ITSM Tickets:</th>      <td tid="product1" ftn="2" id="tt_acvl">NULL</td></tr>
                                        <tr><th>E-Mails:</th>           <td tid="product1" ftn="3" id="em_acvl">NULL</td></tr>
                                        <tr><th>Anrufe:</th>            <td tid="product1" ftn="4" id="ca_acvl">NULL</td></tr>
                                        <tr><th>Skype &amp; Other:</th> <td tid="product1" ftn="5" id="sk_acvl">NULL</td></tr>
                                    </table>
                                </div>

你觉得怎么样? 我认为这只是一件聪明的小事,但我还没有看到任何解决方案......

顺便说一句。我从这里使用 sharepointplus: http://aymkdn.github.io/SharepointPlus/symbols/ $SP%28%29.list.html

亲切的问候 近似

最佳答案

只需将上下文引用存储在变量中以供以后使用:

$(document).ready(function () {
$('td').each(function(index) {
    var self = $(this);  //  <----- this is the key
    thisValue = "";                                     
    currentField = self.attr("tid");
    currentID = self.attr("ftn");

    $SP().list(TableName).get({
        fields: currentField,
        where:"ID = "+currentID
    },function getData(row) {
        console.log (row[0].getAttribute(currentField) *1);
        thisValue = (row[0].getAttribute(currentField) *1);
        self.text(thisValue);
    })

});

关于jquery - 稍后使用 $(this) 不可能,有什么替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35341182/

相关文章:

jquery - 通过 iFrame 提问 Qtip - 使用 JQUERY LIVE

multithreading - D中的 "this"指针和消息接收

JavaScript 方法 : this & that

javascript - 在 CSS 网格布局中填充空单元格

jQuery 选择 e.target 的 child

javascript - 访问 IIFE 中的函数或变量

sharepoint - 在 Sharepoint 中发送电子邮件

sharepoint - SharePoint计时器作业中的“RunWithElevatedPrivileges”?

javascript - 从 getJSON 查询获取 SharePoint Lookup 列的值

c# - 在 C# 中使用带有类名的 “this”