2012-04-06 5 views
0

「hello ??」と入力すると、テキストエリア(id = add_new_comment)に挿入されますhellojQuery1704879437133033947_1333718592556 ??データベース内の。コードの問題は何ですか?あなたのコメントによるとshftに関連するエラー+ Jqueryで入力して入力する

$('#add_new_comment').live('keyup',function (event) { 
       if (event.keyCode == 13 && event.shiftKey) { 
        $(this).val($(this).val()+"\n"); 

        return false; 
       }else if(event.keyCode == 13){ 

        var store_id = ""; 

        store_id = $('.c_store_comment').attr('id'); 

        var new_comment = $.trim($(this).val()); 

        $.ajax({ 
        type:'post', 
        url:path to controller, 
        data:'comment='+new_comment+'&store_id='+store_id, 
        dataType:'json', 
        success:function(vals){ 
         $.each(vals,function(i,values){ 
          switch (i) { 
            case 'error': 
             alert('Sorry Something went Wrong'); 
            break; 

            case 'success': 
             var comment_div = '<div class="col1_comment2_row"><div class="col1_comment2_row_tilte">'+$('.user_name').html()+'</div>'; 
             comment_div += '<div class="col1_comment2_row_img"><a href="#nodo"><img class="delete_comment" id="'+values[1]+'" src="/images/delete_comment.png"></a> </div>'; 
             comment_div += '<div class="col1_comment2_row_cooment">'+values[0]+' </div></div>'; 


             $('.c_store_comment .scroll_container').append(comment_div); 
             $('#add_new_comment').val(''); 

            break; 
           }     
         }); 
        } 
        });   
       } 
      }); 
+0

おかしいチャド! 電子メールパスワードも必要ありませんか? :) – skafandri

+0

それは文字列に "??" ajaxのデータ属性に?すなわち、 data: 'comment =' + new_comment + '&store_id =' + store_id –

+0

は '?'をエスケープします。この場合の文字列ヘルプには?? –

答えて

3

、問題:おかげで、事前に

$('#add_new_comment').live('keyup',function (event) { 

      /*shft+enter for new line*/ 

     if (event.keyCode == 13 && event.shiftKey) { 
      $(this).val($(this).val()+"\n"); 

      return false; 
     }else if(event.keyCode == 13){ 
      /*code to be inserted in db*/ 
     } 
    }); 

データベースのコード(すなわちCodeIgniterの中のモデル。)

function add_new_comment($comment, $store_id, $comment_source,$user_id) { 
     $data = array(
      'comment' => $comment // comment to be inserted. This contains the text of textarea. 
      'source_id' => $store_id, 
      'comment_source' => $comment_source, 
      'from_user_id' => $user_id 
     ); 
     $result = $this->db->insert(COMMENTS, $data); 

     return $this->db->insert_id(); 
    } 

jsのスクリプト(実際のコード)コメントのテキストに疑問符文字がある場合にのみ発生します。

これらの文字は実際にはURLに予約されています(クエリ文字列の先頭にマークされます)。さらに、オブジェクトの代わりにdataオプションの文字列を$.ajax()に渡すので、jQueryはエスケープしないため、処理は実行されません。

なぜjQueryのexpandoプロパティがサーバーにポストされてしまうのかを説明することはできません。ソースコードをすばやく見てもわかりません。しかし、問題を回避する確実な方法は、文字列の代わりにdataオプションでオブジェクトを渡すために、次のようになります。

$.ajax({ 
    type: "POST", 
    url: "path/to/controller", 
    data: { 
     comment: new_comment, 
     store_id: store_id 
    }, 
    dataType: "json", 
    success: function(vals) { 
     // ... 
    } 
}); 

その方法は、jQueryのは自動的にあなたのデータに予約文字をエスケープします、そして、あなたの問題はすべきどこかに行って。

+0

ありがとう..それを試してみて、あなたに知らせてください。 –

+0

明らかに私のこの回答のコメントは彼には十分ではありませんでした:P – Chad

-1
$('#add_new_comment').live('keyup',function (event) { 

     /*shft+enter for new line*/ 

    if (event.keyCode == 13 && event.shiftKey) { 
     $(event.currentTarget).val($(event.currentTarget).val()+"\n"); 

     return false; 
    }else if(event.keyCode == 13){ 
     /*code to be inserted in db*/ 
    } 
}); 
+1

'this'は' live() 'によってイベントターゲットに正しくバインドされていますので、' currentTarget'を使う必要はありません。私はあなたの答えが最初に質問者の問題に対処していないのではないかと心配しています。 –

関連する問題