简体   繁体   English

在jQuery AJAX回调中使用$(this)

[英]Using $(this) inside a jQuery AJAX callback

I have been all over Stack Overflow looking for a solution and none seem to work. 我一直在Stack Overflow上寻找解决方案,但是似乎都没有用。

I cannot seem to figure out the issue. 我似乎无法弄清楚这个问题。 I have a button inside a <td> and on clicking it I want to make an AJAX call to update a database and upon success of that AJAX call I want to update the class of the <td> to mark the button as clicked. 我在<td>有一个按钮,单击它时我想进行AJAX调用以更新数据库,并且在该AJAX调用成功后,我想更新<td>的类以将按钮标记为已单击。

I have tried var that = this; 我试过var that = this; in the function. 在功能上。 I've tried context: this, in the callback. 我已经尝试过context: this,在回调中。

        function setScoreA(event,candidate,rubric,category,score){
            var author = document.getElementById("author").value;
            if(author != ""){
                    $.ajax({
                            context: this,
                            type: "POST",
                            url: "stressBoardUpdate.php",
                            data: "candidate="+candidate+"&category="+category+"&score="+score+"&author="+author,
                            success: function(){
                                    $(that).parent('td').siblings().removeClass('isScore');
                                    $(that).parent('td').addClass('isScore');
                            }
                    });
            }else{
                    alert("Author must contain something...");
            }
         }

Here is how the function would get invoked. 这是调用函数的方式。

<input type="button" "="" value="5" onclick="setScoreA('Stress Board','Y235','Stress Board Rubric','Handled Stress','5');">

onclick="setScoreA does not set this to the element clicked but rather to window . The way you are using it. The way you are using it, I'm not sure that you could actually get a reference to the element. Instead of using onclick , you should bind an event listener (which you can do with jQuery anyway): onclick="setScoreA不设置this为点击的元素,而是以window 。您正在使用它的方式。你使用它的方式,我不知道你实际上可以得到元素的引用,而是使用onclick ,您应该绑定一个事件侦听器(无论如何您都可以使用jQuery):

$("input").on("click", function () {
    setScoreA(this, 'Stress Board','Y235','Stress Board Rubric','Handled Stress','5');
});
function setScoreA(element, ...
    /* snip */
    context: element

If you really wanted to stick with this for some reason, you could use: 如果出于某些原因您确实想坚持使用this方法,则可以使用:

setScoreA.call(this, 'Stress Board' ...

First of all, make use data attributes in your code and setup a common .click() listener 首先,在代码中使用data属性并设置一个通用的.click()侦听器

HTML: HTML:

<input type="button" class=".button-group" data-event="Stress Board" data-candidate="Y235" data-rubric="Stress Board Rubric" data-category="Handled Stress" data-score="5">

jQuery: jQuery的:

$(".button-group").click(function() {
    // Do something
});

Also, I presume you are dynamically generating many buttons. 另外,我想您正在动态生成许多按钮。 The code above could be improved having only 1 click listener for the whole table, rather setting up a click listener for each item. 可以改进上面的代码,使整个表只有1个单击侦听器,而不是为每个项目设置一个单击侦听器。

$("#wrapper").on("click", "input", function() {

    var event = $(this).data("event");
    var candidate = $(this).data("candidate");
    var rubric = $(this).data("rubric");
    var category = $(this).data("category");
    var score = $(this).data("score");

    setScoreA(this, event, candidate, rubric, category, score);

});

JSFIDDLE DEMO JSFIDDLE演示

Resources: 资源:

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

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