如何在x秒后自动提交表单?

时间:2016-08-27 13:37:54

标签: javascript jquery html codemirror

我想在x秒后自动提交表单而不按提交按钮。 This sample运行良好,但当我将此示例放入my code时,它确实无法运行。

我的代码出了什么问题?

CodeMirror.commands.autocomplete = function(cm) {
    CodeMirror.showHint(cm, CodeMirror.hint.anyword); 
};


var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    matchBrackets: true,
    mode: "text/x-stsrc",
    indentUnit: 4,
    autoCloseBrackets: true,
    highlightSelectionMatches: true,
    styleActiveLine: true,
    extraKeys: {
        "Ctrl-Space": "autocomplete" ,
        "Ctrl-Space":function(cm){  CodeMirror.showHint(cm, CodeMirror.hint.anyword); }, 
        "'.'" : function(cmm){ CodeMirror.showHint(cmm, CodeMirror.hint.anyword);},
    },
    autohint: true,
    readOnly: false,
    lineWrapping: true,
}); 

editor.on("blur", function(codeMirror) { codeMirror.save(); });

editor.refresh();

var orig = CodeMirror.hint.anyword;

CodeMirror.hint.anyword = function(cm) {
    var inner = orig(cm) || { from: cm.getCursor(), to: cm.getCursor(),  list: []};
    inner.list.push("mariano");
    inner.list.push("florencia");
    inner.list.push("zoe");

    return inner;
};
var timeoutId;
$('form input, form textarea').on('input propertychange change', function() {
    console.log('Textarea Change');

    clearTimeout(timeoutId);
    timeoutId = setTimeout(function() {
        // Runs 1 second (1000 ms) after the last change    
        saveToDB();
    }, 1000);
});

function saveToDB()
{
    console.log('Saving to the db');
    form = $('.contact-form');
    $.ajax({
        url: "/echo/json/",
        type: "POST",
        data: form.serialize(), // serializes the form's elements.
        beforeSend: function(xhr) {
            // Let them know we are saving
            $('.form-status-holder').html('Saving...');
        },
        success: function(data) {
            var jqObj = jQuery(data); // You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
            // Now show them we saved and when we did
            var d = new Date();
            $('.form-status-holder').html('Saved! Last: ' + d.toLocaleTimeString());
        },
    });
}

// This is just so we don't go anywhere  
// and still save if you submit the form
$('.contact-form').submit(function(e) {
    saveToDB();
    e.preventDefault();
});

CodeMirror.hint.anyword = function(cmm) {
    var inner1 = orig(cmm) || { from: cmm.getCursor(), to: cmm.getCursor(),  list: []};
    inner1.list.push("one");
    inner1.list.push("tow");
    inner1.list.push("three");

    return inner1;
};

编辑: 这个改变解决了这个问题:

var timeoutId;
$('form input, form textarea').on('input propertychange change', function() {
    console.log('Textarea Change');
    //alert("chang Comment!");

    clearTimeout(timeoutId);
    timeoutId = setTimeout(function() {
        // Runs 1 second (1000 ms) after the last change    
        document.forms["contact-form"].submit();
    }, 1000);
});

3 个答案:

答案 0 :(得分:1)

首先:感谢每个人回答我 第二:问题只能通过添加解决:

document.forms["id-form"].submit();

答案 1 :(得分:0)

在你的小提琴中添加Jquery。如果您在小提琴中打开控制台。你可以看到错误信息

add_index :users, :authentication_token, :unique => true

包括Jquery,问题将得到解决。

答案 2 :(得分:0)

您需要使用code : JSON.parse('{"a":"\\\\"}'); node: { a: '\\' } chrome: {a: "\"} editor事件: -

change

JS Bin