Grails命令的功能执行

时间:2014-08-27 20:44:17

标签: javascript jquery grails

我有一个formRemote,当它像这样提交时,它会在我的控制器中调用一个函数:

<g:formRemote name="editIndivRecForm" url="[controller: 'customer', action:'saveEditedIndividualRecord']" onSuccess="doResult();">

点击按钮提交此表单。相反,点击一个名为'save'的按钮会在通过Javascript点击表单的提交按钮之外执行其他操作。以下是此按钮的单击处理程序:

$('#save').click(function () {
            $("#uniqueId").prop('disabled', false); // Have to enable before form submission else it doesn't go back as a param to controller.
            $("#secondaryId").prop('disabled', false);
            $("#submit").trigger("click"); // formRemote's submit button
            $('#editIndivRecForm').reset;
            <g:remoteFunction controller="customer" 
                                        action="remediationSearch" 
                                        update="content_area" 
                                        params="{rerender: true}"/>
        });

我遇到的问题是我需要点击处理程序remediationSearch调用我的控制器的功能,以便在formRemote的提交saveEditedIndividualRecord调用的控制器的功能完成后运行。但它正在发生反过来。由于某种原因,函数onSuccess="doResult();"甚至没有执行,否则我将把以下代码移到它的主体中,以便按照我想要的方式工作:

<g:remoteFunction controller="customer" 
                                            action="remediationSearch" 
                                            update="content_area" 
                                            params="{rerender: true}"/>

以下是doResult现在的情况:

function doResult() {
            console.log("done.");
        }

提交了formRemote,但doResult函数没有向控制台打印任何内容。

1 个答案:

答案 0 :(得分:1)

看到所有与Grails AJAX相关的标签have been deprecated,我建议您这样做:

标记:

<form id="editIndivRecForm" onsubmit="return false;">
  <!-- add fields here -->
  <input type="text" id="uniqueId" value="${something}">
  <input type="text" id="secondaryId" value="${something}">
  <button id="save" type="button">
</form>

JavaScript的:

// Function to update your content_area div
function updateContentArea() {
    var params = { rerender: true };
    var url = "${createLink(controller: 'customer', action: 'remediationSearch')}";
    $.get(url, params, function(data) { 
        $("#content_area").empty().append(data); 
    });
}

$("#save").on('click', function() {
    // Collect values from form and submit ajax request
    // Using name and description for example fields here:
    var data = {
        name: $("#name").val(),
        description: $("#description").val(),
        uniqueId: $("#uniqueId").val(),
        secondaryId: $("#secondaryId").val()
    };
    var url = "${createLink(controller: 'customer', action: 'saveEditedIndividualRecord')}";

    // Submit the (first) AJAX request
    $.ajax({
        type: "post",
        url: url,
        data: data,
        success: function() {
            doResult();
            $('#editIndivRecForm').reset();
            updateContentArea();
        }
    });
}