JQuery对话框 - 访问部分表单值

时间:2011-05-13 07:41:33

标签: jquery json forms dialog

您好我加载了一个JQuery对话框,其中包含一个带有(目前)一个输入字段的表单。

我正在使用MVC 3和JsonValueProviderFactory来支持将JSON传递给我的action方法。但是我无法访问我的表单字段,因为对话框正在加载部分字段。

有没有人知道用于访问加载到对话框中的表单字段的JQuery。对话框代码是:

$('#Test').dialog({
        bgiFrame: true,
        autoOpen: false,
        modal: true,
        height: 400,
        width: 500,
        title: 'Add report',
        draggable: true,
        postion: 'center',
        buttons: {
            "save": function () {

                $.ajax({
                    url: '/Test/Save',
                    type: "Post",
                    data: JSON.stringify(data),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function () {
                        alert("well done");
                    },
                    error: function () {
                        alert("error");
                    }
                });
            },
            "cancel": function () {
                $(this).dialog('close');
            }
        }
    });

正如您所看到的,我使用JSON.stringify(数据)但没有定义数据,因为我需要从表单值构造一个类型。顺便说一下,当我这样做时它起作用,因为数据变量代表了我的action方法接收的类型,但是我想从表单字段构造它

$('#Test').dialog({
        bgiFrame: true,
        autoOpen: false,
        modal: true,
        height: 400,
        width: 500,
        title: 'Add report',
        draggable: true,
        postion: 'center',
        buttons: {
            "save": function () {

                var data = { Name: "Blah" }; 

                $.ajax({
                    url: '/Test/Save',
                    type: "Post",
                    data: JSON.stringify(test),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function () {
                        alert("well done");
                    },
                    error: function () {
                        alert("error");
                    }
                });
            },
            "cancel": function () {
                $(this).dialog('close');
            }
        }
    });    

任何人都可以提供任何帮助,我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

好的,如何在DOM上进行操作,而不显示表单,例如:

var clone =  $('form .partial').clone().wrap('<form />').parent();    
var data = clone.serialize();
clone.remove();//remove the clone from the dom, dispose of it as soon as we gathered the data.
//this will stop any ID duplication as @redsquare pointed out.

$.ajax({ 
   url: '[url]',
   'data':data,
   //...success,error, type, etc ... 
})

答案 1 :(得分:0)

我建议你在部分加载到对话框中创建一个表单标签。然后,您可以使用jquery serialize方法创建要发送到操作方法的帖子值。

e.g

var data = $('#formId').serialize();
相关问题