簡體   English   中英

使用用戶輸入的文本字段值更新數組

[英]Update an array using a text field value that a user enters

我正在使用Rails應用程序(版本:2.3.8)ruby版本1.8.7,並且我有一張表列出了特定類示例的所有記錄。

視圖

<h2> purifications in last <%= text_field_tag "number", @n %> days </h2>

<div class="purifications"> 
 <table>
    <%@purifications.each do |r|%>
     <tr>
       <td><%= @purification.name %></td>
     </tr>
    <%end%>
 </table>
</div>

調節器

def purifications
    @n = 30
    @purifications = Purification.find(:all, :conditions => ["end_date > ?", @person.id, @n.days.ago.to_s(:db)])
end

我想使用Jquery或javascript和ajax使用遠程方法更新此方法。 有人可以幫我怎么做嗎?

我嘗試使用

視圖

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 <script> 
  $j = jQuery.noConflict(true);

    $j(document).ready(function(){
     $j("number").change(function update_table(){
        $j.post("/protein_purification/remote_test",
          {
          number_for_update: $('this').value,
          });
        }); 
     });


</script>

調節器

def remote_test
    raise params.inspect
end

我根本無法傳遞任何參數,控制台也沒有給出任何錯誤。 我不知道這是如何工作的,因為我是ajax的新手。 我正在使用Rails 2.3.8進行工作,是否有任何更簡單的方法可以解決? 謝謝

您的選擇器有問題:

$j(document).ready(function(){
 $j("number").change(function update_table(){
   # ^ Here, it is trying to find a HTML element <number></number>

您應該改用以下選擇器:

$j(document).ready(function(){
 $j("#number").change(function update_table(){
   # ^ this will try to find any html element matching id 'number'

另外,我不確定,但我認為您不能在這樣的回調中定義函數,請嘗試以下方法:

$j(document).ready(function(){
 $j("#number").change(function () {
    $j.post("/protein_purification/remote_test",
      {
        number_for_update: $('this').value,
      });
    }); 
 });

或等效的:

$j(document).ready(function(){
  $j("#number").change( update_table($(this).val()) );
});

window.update_table = function (value) {
    $j.post("/protein_purification/remote_test",
      { number_for_update: value });
    });      
    # this function is clearly unfinished
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM