1

Web サイトのコメント システムに取り組んでいますが、フィールド全体の値を取得できません。

このスクリプトを使用していますhttps://github.com/podio/jquery-mentions-input

return {
  init : function (options) {
    settings = options;

    initTextarea();
    initAutocomplete();
    initMentionsOverlay();
  },

  val : function (callback) {
    if (!_.isFunction(callback)) {
      return;
    }

    var value = mentionsCollection.length ? elmInputBox.data('messageText') : getInputBoxValue();
    callback.call(this, value);
  }
}

関数 val は値を返すものです!

これは、値を取得するために使用しているコードです。(うまくいきません)

 $('#commentInput').mentionsInput('val', function(comment){ var test = comment; });
     alert( test );

この関数から値コメント/テストを抽出して、データベースに送信できるようにする方法を見つけたいと思います。

この次のコードを使用すると、アラート ボックスに適切な情報が表示されます。

('#commentInput').mentionsInput('val', function(comment) { alert(comment); }); 

他のいくつかの操作を試してみましたが、[object Object] や [HTMLDivElement] などのエラーが常に発生していました。

ばかばかしいほど簡単なことだと確信していますが、理解できません。

誰かが私を助けてくれることを願っています

良い一日を過ごしてください

ヨリス

4

1 に答える 1

0

ジョリス、

('#commentInput').mentionsInput('val', function(comment) { alert(comment); });正しい定式化です。次のように、さらに一歩進む必要があります。

('#commentInput').mentionsInput('val', function(comment) {
    sendToDatabase(comment);
});

ここsendToDatabaseで、引数としてコメントを受け取り、それを(ajaxとサーバースクリプトを介して)データベースにアップロードする関数は次のようになります。

function sendToDatabase(comment){
    $ajax({
        url: 'path/to/server/script',
        type: "GET", //or "POST"
        data: { "comment": comment },
        success: function(){
            //here display something to confirm that the upload was successful
        },
        error: function(){
            //here display something to indicate that the upload was not successful
        },
    });
}
于 2012-04-23T21:55:13.147 に答える