javascript - jquery函数中的目标AJAX id

标签 javascript jquery ajax

我的脚本是这样的:

function comtype($id, $comtype, $vendor){
        //get the input value
        $.ajax({
            //the url to send the data to
            url: "example.com/updatedb.php",
            //the data to send to
            data: {id: $id, comtype: $comtype, vendor: $vendor},
            //type. for eg: GET, POST
            type: "POST",
            //on success
            success: function(data){
                console.log("***********Success***************"); //You can remove here
                console.log(data); //You can remove here
                (function($) {              
                  jQuery.fn.extend({
                    prependClass: function(newClasses) {
                      return this.each(function() {
                        var currentClasses = $(this).prop("class");
                        $(this).removeClass(currentClasses).addClass(newClasses + " " + currentClasses);
                      });
                    }
                  });

                })(jQuery);
                $("#mycontent a.half").removeClass("half").prependClass("paid");
                //$("#mycontent a.pending").removeClass("pending").prependClass("half");

            },
            //on error
            error: function(){
                    console.log("***********Error***************"); //You can remove here
                    console.log(data); //You can remove here
            }
        });
    }

对于这个特定的行:

$("#mycontent a.half").removeClass("half").prependClass("paid");

我希望能够定位使用该 id 的确切类。

例如,我有几个类的链接,例如:

class="half first4234"
class="half first8293"
class="half first9221"

我尝试过以下方法:

$("#mycontent a.half first$id").removeClass("half").prependClass("paid");

这会引发以下错误:

Error: Syntax error, unrecognized expression: #mycontent a.half first$id

如何获取 $id 并使其正常工作?

最佳答案

您需要将变量连接到选择器字符串

$("#mycontent a.half first" + $id)

关于javascript - jquery函数中的目标AJAX id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519350/

相关文章:

javascript - 在 Mac 上将 Web 应用程序转换为全屏桌面应用程序?

c# - 为什么我的 MVC 应用程序的局部 View 中出现字典错误?

javascript - 如何使用AJAX从php获取变量

javascript - JS : Nested functions gain a copy of the outer function's parameters?

Javascript - 将 dom 元素引用转换为节点

javascript - React-dom 的钩子(Hook)调用无效。我能做些什么?

javascript - 无法渲染在 jQuery $.get 请求函数中添加到场景的三个 js 对象

javascript - 纯 javascript 检查是否有东西悬停(不设置鼠标悬停/移出)

ajax - JSF 在进行 ajax 更新时重置字段

javascript - 重定向时自动选择选项卡