以后使用$(this)不可能的替代方案是什么?

时间:2016-02-11 13:51:04

标签: 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页面中的tablecell我认为。 这是我的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

亲切的问候 接近

1 个答案:

答案 0 :(得分:3)

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

$(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);
    })

});