2012-04-23 47 views
1

我正在爲我的網站評論系統工作,我無法檢索整個字段的值。jquery提到輸入:輸出數據問題

我使用這個腳本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); }); 

我嘗試了一些其他的操作,以試圖使它的工作,但我總是得到一樣的錯誤[對象的對象]或[HTMLDivElement]

我敢肯定這是愚蠢的東西很容易,但我無法弄清楚。

希望有人能夠幫助我

有一個好的一天

里斯

回答

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 
     }, 
    }); 
} 
+0

非常感謝,它工作完美。但我仍然想知道爲什麼你不能提取這個值並把它放入一個變量中? 再次感謝@甜菜根 - 甜菜根,祝你有美好的一天! – 2012-04-23 23:27:04

+0

定義爲'.mentionsInput()'的第二個參數的函數是一個帶有形式變量'comment'的回調函數。 'comment'的範圍侷限於回調函數。你試圖訪問'comment'的地方,它超出了範圍,但還沒有設置。 – 2012-04-24 03:35:47