Jquery 获取表单id,然后将其用作div名称

Jquery 获取表单id,然后将其用作div名称,jquery,Jquery,我在一页上有一系列的表格。我试图获取提交的特定表单的id,并使用该确切值作为div的名称,以便在其中插入一些html。具有该名称的div已存在 代码如下: $(function () { $('.allforms').submit(function (ev) { Var myID = $(obj).attr("id"); esv.preventDefault(); $.ajax({ url: "/update",

我在一页上有一系列的表格。我试图获取提交的特定表单的id,并使用该确切值作为div的名称,以便在其中插入一些html。具有该名称的div已存在

代码如下:

$(function () {
    $('.allforms').submit(function (ev) {
        Var myID = $(obj).attr("id");
        esv.preventDefault();
        $.ajax({
            url: "/update",
            data: $(this).serialize(),
            dataType: "html",
            success: function (data) {
                $(myID).html(data);
            }
        });
    });
});
当我给div赋予相同的名称时,数据返回正确,但只在第一个div中显示,因此我知道服务器端正在工作。我刚刚开始使用Jquery。

更改这些:

Var myID = $(obj).attr("id"); // 'var' keyword should be lowercase
esv.preventDefault(); // you are passing event object as `ev` not `esv`
致:

这将返回表单的ID

使用

$(function () {
    $('.allforms').submit(function (ev) {
        Var myID = this.id; // this refers to the form, and id is a droperty of the element so you can access it directly..
        ev.preventDefault(); // esv -> ev as that is the name you use as the parameter
        $.ajax({
            url: "/update",
            data: $(this).serialize(),
            dataType: "html",
            success: function (data) {
                $('#' + myID).html(data);
            }
        });
    });
});

这几乎做到了。非常感谢这让我头疼。我需要将最后一部分编辑为:$(“div#“+myID).html(数据);为了确保它将数据放入正确的div中,而不是表单中。@geof,id属性在文档中必须是唯一的。。最好使用固定前缀,使它们在表单和容器之间唯一。。比如表单
id=“yourid”
和div
id=“yourid container”
。这将使文档有效,您可以使用
$('#'+myID+'-container').html(数据)将其作为目标
var myID = $(this).attr("id");
$(function () {
    $('.allforms').submit(function (ev) {
        Var myID = this.id; // this refers to the form, and id is a droperty of the element so you can access it directly..
        ev.preventDefault(); // esv -> ev as that is the name you use as the parameter
        $.ajax({
            url: "/update",
            data: $(this).serialize(),
            dataType: "html",
            success: function (data) {
                $('#' + myID).html(data);
            }
        });
    });
});