2012-03-02 38 views
1

每次有人在單選按鈕中更改其響應時,它都會記錄在數據庫中。下面是在application.js文件中的javascript:render:nothing => true在jquery mobile Rails 3.0應用程序中不起作用

$('.submittable').live('change', function() { 
    $(this).parents('form:first').submit(); 
    return false; 
}); 

在單選按鈕HTML是:

class="submittable" 

在完整的網站有頁面沒有改變,因爲它是在像這樣的控制器抑制:

def update_result 
    ... 
    render :nothing => true 
end 

然而,在移動版本的頁面翻轉到一個頁面,上面寫着在左上角undefined但在其他方面是空白。終端窗口消息:

... 
Processing by AnswersController#update_result as JS 
... 
Rendered text template (0.0ms) 
Completed 200 OK in 309ms (Views: 5.6ms | ActiveRecord: 5.1ms) 

感謝您的幫助。

回答

1

使用jQuery Mobile的,提交由自己一個形式(所以沒有換頁時),我們要設置data-ajax="false"<form>標籤,然後設置我們自己的AJAX功能(在submit回調形式):

//as of jQuery 1.7 `.live()` is depreciated in favor of `.delegate()` 
$(document).delegate('.submittable', 'change', function() { 

    //`.closest('form')` is the same as `.parent('form:first')`, also `.submit()` is shorthand for `.trigger('submit')`, just FYI 
    $(this).closest('form').trigger('submit'); 
    return false; 
}).delegate('form', 'submit', function() { 

    //cache the jQuery object of this form and use that variable to setup the AJAX request 
    var $form = $(this); 
    $.ajax({ 
     url  : $form.attr('action'),//use the form's action attribute for the URL of the request 
     type : $form.attr('method'),//use the form's method attribute to set the TYPE of the request 
     data : $form.serialize(),//add the form input data to the request 
     success : function (serverResponse) { 
      //the server has responded, whatever was output by the server-side script is available through the `serverResponse` variable 
     }, 
     error : function (jqXHR, textStatus, errorThrown) { 
      //make sure to handle errors 
     } 
    }); 

    return false; 
}); 

<form action="..." data-ajax="false" method="post"> 
    ... 
</form> 
+0

感謝您的所有工作。我得到的邏輯,它是有道理的,但我一直想知道是否有一個更簡單的解決方案? – Jay 2012-03-02 20:12:55

相關問題