简体   繁体   English

Javascript不提交来自动态生成表单的数据

[英]Javascript not submitting data from a dynamically generated form

When a user clicks on a button it loads a form (#CtrlST) into the usercontent div. 用户单击按钮时,会将表单(#CtrlST)加载到usercontent div中。 Then the user fills out the form and submits it. 然后,用户填写表格并提交。 The issue is that it is not able to trigger the ctrlst.onsubmit function to process the form data. 问题在于它无法触发ctrlst.onsubmit函数来处理表单数据。 When the form is hard coded, the submit function fires nicely. 对表单进行硬编码后,submit函数会很好地触发。 However, the dynamically generated form doesn't. 但是,动态生成的表单却没有。 I need the form to be dynamically generated as the rest of the code I need to write needs work in the same way. 我需要动态生成表单,因为我需要编写的其余代码需要以相同的方式工作。

window.onload = function() {
// Get references to elements on the page.
var StartGB = document.getElementById('StartGB');
var socketStatus = document.getElementById('usercontent');
var ctrlst = document.getElementById('CtrlST');
var mySocket = new WebSocket("ws://0.0.0.0:5678/");

if (StartGB) {
    StartGB.addEventListener("click", function (e) {
        var gbform = document.getElementById('usercontent');
        gbform.insertAdjacentHTML('afterbegin','\n<form class="CST" id="CtrlST" action="#" method="post">\n'+
        '<div class="grid-container-plus">\n'+
        '<div class="grid-item"><input id="goodreads" name="goodreads" placeholder="Good"></div>\n'+
        '<div class="grid-item"><input id="badreads" name="badreads" placeholder="Bad"></div>\n'+
        '<div class="grid-item"><button type="submit" class="sbutton">&laquo; Start &raquo;</button></div>\n'+
        '</div>\n'+
        '</form>\n');
    })

}

if (ctrlst) {
    alert('wtf')
    // Send a message when the form is submitted.
    ctrlst.onsubmit = function(e) {
        e.preventDefault();
        // Retrieve the message from the Good /Bad form.
        var message = good.value + ':' + bad.value;
        // Send the message through the WebSocket.
        alert(message);
        mySocket.send(message);
        // Add the message to the messages list.
        socketStatus.innerHTML += '<div class="received">' + message + '</div>';
        return false;
    };
}
};

Actually if you are attaching event to the DOM, that DOM object should be available already. 实际上,如果将事件附加到DOM,则该DOM对象应该已经可用。 So your first case with hard coded form is working properly. 因此,您的第一个带有硬编码形式的案例可以正常工作。

For the second case if you are injecting the Form related DOM dynamically, event also should be attached after that HTML injection. 对于第二种情况,如果要动态注入与表单相关的DOM,则在注入HTML之后也应附加事件。

Modified your code as follows to attach submit event dynamically, This will work for your case, 修改代码如下,以动态附加提交事件,这将适合您的情况,

window.onload = function() {
// Get references to elements on the page.
var StartGB = document.getElementById('StartGB');
var socketStatus = document.getElementById('usercontent');
var mySocket = null; //new WebSocket("ws://0.0.0.0:5678/");

if (StartGB) {
    StartGB.addEventListener("click", function (e) {
        var gbform = document.getElementById('usercontent');
        gbform.insertAdjacentHTML('afterbegin','\n<form class="CST" id="CtrlST" action="#" method="post">\n'+
        '<div class="grid-container-plus">\n'+
        '<div class="grid-item"><input id="good" name="goodreads" placeholder="Good"></div>\n'+
        '<div class="grid-item"><input id="bad" name="badreads" placeholder="Bad"></div>\n'+
        '<div class="grid-item"><button type="submit" class="sbutton">&laquo; Start &raquo;</button></div>\n'+
        '</div>\n'+
        '</form>\n');
        addSubmitEvent();
    })

}
addSubmitEvent();
};
function addSubmitEvent(){
var ctrlst = document.getElementById('CtrlST');
if (ctrlst) {
    alert('wtf')
    // Send a message when the form is submitted.
    ctrlst.onsubmit = function(e) {
        e.preventDefault();
        // Retrieve the message from the Good /Bad form.
        var message = good.value + ':' + bad.value;
        // Send the message through the WebSocket.
        alert(message);
        mySocket.send(message);
        // Add the message to the messages list.
        socketStatus.innerHTML += '<div class="received">' + message + '</div>';
        return false;
    };
}
}

There is one more concept called delegation for such kind of dynamic elements event handling, please go through for your reference. 对于这种动态元素事件处理,还有一个称为委托的概念,请仔细阅读以供参考。 If you use JQuery, it will be easy with delegation. 如果使用JQuery,则委派将很容易。

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

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