简体   繁体   English

动态Javascript表单输入不发布

[英]Dynamic Javascript form input not POSTing

I am working on a web application using the python-flask framework. 我正在使用python-flask框架开发Web应用程序。

A trouble I am having is on a form where the user can create new input fields. 我遇到的麻烦是用户可以在其中创建新输入字段的表单上。 The forms appear in the HTML code, and visually appear in the browser, however the elements that are created dynamically do not appear in the POST request. 表单以HTML代码形式出现,并以可视方式出现在浏览器中,但是动态创建的元素不会出现在POST请求中。

Below is two javascript functions, the first creates the form element when the user pushes the appropriate button. 以下是两个javascript函数,第一个在用户按下相应按钮时创建form元素。 The second code I found online, because it said that the serialize would add the Dynamic form elements to the POST request. 我在网上找到的第二个代码,因为它说序列化会将动态表单元素添加到POST请求中。

$(function create_new_row_plus_button_fn() {
$("#addRows").click(function () {
            counter = counter + "a";
            var trelem = document.createElement('tr');
            var tdelem = document.createElement('td');
            trelem.appendChild(tdelem);
            var frm = document.getElementById("new_table_id");
            var newEl = document.createElement("input");
            newEl.name = counter;
            newEl.type = "text";
            tdelem.appendChild(newEl);
            frm.appendChild(trelem);
    });
})

$('#form_container').on('submit', function(e) {
    //prevent the default submithandling
    e.preventDefault();
    //send the data of 'this' (the matched form) to yourURL
    $.post('/add_new_product', $(this).serialize());
    location.reload();
});

One thing that I notice is that if I view html from the chrome debugger, the dynamic form elements are viewable, but if I look at the page source they are not. 我注意到的一件事是,如果我从chrome调试器中查看html,则动态表单元素是可见的,但是如果查看页面源,则看不到它们。

The problem occurred from having the element inside of the . 问题发生在元素内部。 Moving the outside the fixed the issue. 将外部移动解决了该问题。

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

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