简体   繁体   English

x秒后如何自动提交表格?

[英]How to submit form automatically after x seconds?

I want to submit form automatically after x seconds without pressing the submit button. 我想在x秒后自动提交表单,而无需按提交按钮。 This sample runs fine, but when I put this sample in my code , it did't work. 该示例运行良好,但是当我将该示例放入代码中时 ,它不起作用。

What's wrong with 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;
};

EDIT : the problem solved by this change : 编辑:此更改解决的问题:

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);
});

First : thanks for every body answer me 第一:谢谢大家的回答
second : the problem solved only by add : 第二:只能通过添加解决问题:

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

Add Jquery in your fiddle. 在您的小提琴中添加Jquery。 If you open console in your fiddle. 如果您在小提琴中打开控制台。 you can see the error message 您可以看到错误消息

ceyanebepa.js:45 Uncaught ReferenceError: $ is not defined

Include Jquery and the problem will be solved. 包含Jquery即可解决问题。

you need to use the editor change event:- 您需要使用editor change事件:-

editor.on('change', function() {

JS Bin JS斌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM