0

我正在尝试将 2 个参数传递给 ajax,但我不知道该怎么做。它可以通过id但不通过anotherId. 我已经查看了.on() 文档,但并没有真正帮助我。

HTML

<button class="btn btn-sm btn-success edit_modal" id="123" anotherId="111">Click</button>

JS

$(document).on('click', '.edit_modal', function() {
            var id1 = this.id;
            var id2 = this.anotherId;
            console.log(id1); //this returns "123" in console
            console.log(id2); //this returns undefined...why?

            $.ajax({
                    ...
                    data: { "id1":id1, "id2":id2 }
                    ...
            });    
});
4

1 回答 1

2

改变这两个:

var id1 = this.id;
var id2 = this.anotherId;

至:

var id1 = $(this).attr("id");
var id2 = $(this).attr("anotherId");
于 2018-07-30T15:33:40.520 回答