2011-08-01 13 views
0

私は送信時にURLがどこにあるのかをajaxリクエストを作成しようとしています。Ajaxリクエストを作成するJqueryヘルプ

私のjQuery:

$(document).ready(function() { 
$('.menuitem input:checkbox').change(function(){ 
     $.ajax({ 
      type:'get', 
      url:form.submit();, 
      data:form.serialize(), 
      success:function(msg){ 
      $('#formcontent').html(msg); 
      } 
     }) 
    }) 
    $('input:submit').hide(); 

    $('.menuitem input:checkbox').change(function(){ 
    if($(this).is(":checked")) { 
     $('div.hidediv').removeClass("hidediv"); 
    } 
}); 
}); 

答えて

1
$('.menuitem input:checkbox').change(function() { 
    // fetch the form containing the checkbox whose value 
    // has just changed so that we could infer its action and method 
    var form = $(this).closest('form'); 
    $.ajax({ 
     url: form.attr('action'), 
     type: form.attr('method'), 
     data: form.serialize(), 
     success: function(msg) { 
      $('#formcontent').html(msg); 
     } 
    }); 
}); 
+0

の入力を得るための間違った方法を使用今、私は二重のメニューを取得し、どのように私は唯一のdiv formcontent –

+0

@Rails初心者内のコンテンツを取得するか、厳密に全く分からないものをダブルメニューあなたが話していることは、HTMLがあなたのサーバーから返されていることと、formcontent divの中に入るコンテンツです。 –

+0

私はidのformcontentでdiv内のコンテンツを返すだけです。そうでなければ、すべてのページをformcontentの中に返すようにします。 –

0

あなたが持っている行:

url: form.submit;, 

は次のようになります。

url: form.attr('action'), 

あなたがで終わる:

$.ajax({ 
     type:'get', 
     url:form.attr('action'), 
     data:form.serialize(), 
     success:function(msg){ 
     $('#formcontent').html(msg); 
     } 
    }) 

一貫性のために、 "type"値をform.attr( 'method')に変更することもできます。

1

固定コード

$(document).ready(function() { 
    $('.menuitem input:checkbox').change(function(){ 
     var form = $(this).parents('form'); 
     $.ajax({ 
      type:'get', 
      url:form.attr('action'), 
      data:form.serialize(), 
      success:function(msg){ 
      $('#formcontent').html(msg); 
      } 
     }); 

    $('input[type="submit"]').hide(); 

    $('.menuitem input[type="checkbox"]').change(function() { 
     if($(this).is(':checked')) { 
      $('div.hidediv').addClass('someclass'); // not needed it there is another way of identifying the div later to add to hidediv class again. 
      $('div.hidediv').removeClass('hidediv'); 
     } else { 
      $('div.someclass').addClass('hidediv'); 
     } 
    }); 
}); 

説明:

  1. それがどこにも定義されていないformは何ですか。
  2. formの場合は定義されていると、あなたは、フォームをform.submit();提出するん(あなたが望むものではない。)
  3. あなたはまた、Ajaxの機能の後にセミコロンが欠落しています。
  4. あなたはまた、特定の種類
+0

チェックボックスのチェックを外したときにクラスを追加するには? –

+0

@Rails初心者:1秒私は私の答えを更新します – PeeHaa

+0

@Rails beginner:私の更新された答えをチェックしてください – PeeHaa

関連する問題