【问题标题】:Jquery mention input : output data issueJquery提及输入:输出数据问题
【发布时间】:2012-05-04 13:01:58
【问题描述】:

我正在为我的网站开发我的评论系统,但我无法检索整个字段的值。

我正在使用这个脚本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] 之类的错误

我很确定这很简单,但我想不通。

希望有人可以帮助我

祝你有美好的一天

乔里斯

【问题讨论】:

    标签: jquery comments return underscore.js


    【解决方案1】:

    乔里斯,

    ('#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
            },
        });
    }
    

    【讨论】:

    • 非常感谢,它运行良好。但我仍然想知道为什么你不能只提取值并将其放入变量中?再次感谢@Beetroot-Beetroot,祝您有美好的一天!
    • .mentionsInput()的第二个参数定义的函数是一个带有形式变量comment的回调函数。 'comment' 的范围仅限于回调函数。您尝试访问 comment 的位置超出范围,但尚未设置。
    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多