简体   繁体   English

提交动态生成的表单

[英]Submitting the dynamically generate form

What I am trying to do is creating a new form dynamically based on some input, once that is done I have given a submit button to submit the form. 我想做的是根据一些输入动态地创建一个新表单,一旦完成,我就给了一个提交按钮来提交表单。

Here's the code: 这是代码:

function add(index_array) {
    //create the form
    var myform = document.createElement("form");
    myform.id="k_form"
    for ( i =0 ; i <index_array.length ; i ++)
    {
        var mytext = document.createElement("input");
        mytext.tpye="text";
        mytext.value=index_array[i];
        mytext.id=index_array[i];
        myform.appendChild(mytext); 

    }
    var mybutton = document.createElement("input");
    mybutton.type="button"
    mybutton.value="submit"
    myform.appendChild(mybutton);
    mydiv=document.getElementById("d_div");
    mydiv.appendChild(myform);
}

This creates the form, now I am trying to process the inputs to this form when the user clicks submit. 这将创建表单,现在,当用户单击“提交”时,我正在尝试处理对此表单的输入。

How can i do that? 我怎样才能做到这一点?

I was thinking I could call the function 我以为我可以调用该函数

document.forms["myform"].submit() 

But how can I make it trigger when user clicks submit? 但是,当用户单击提交时,如何触发它?

Please help :) 请帮忙 :)

Add a validation function: 添加验证功能:

function add(index_array) {
var myform = document.createElement("form");
$(myform).attr('id',"k_form");
for ( i =0 ; i <index_array.length ; i ++)
{
    var mytext = document.createElement("input");
    $(mytext).attr('type','text').attr('value',index_array[i]).attr('id', index_array[i]);
    $(myform).append(mytext); 
}
var mybutton = document.createElement("input");
$(mybutton).attr('type','submit').attr('value','submit').attr('onclick','return display_textarea();');
$(myform).append(mybutton); 
mydiv=document.getElementById();
$("#d_div").append(myform);
}

function display_textarea(){
 var validation_success = true;
//do your validation here;
if(validation_success)
    return true;//submits the form
else {
     alert('validation not success');
    return false;// will not submit the form
    }
}

add onclick="doSomeThing();" 添加onclick =“ doSomeThing();” top your button 在您的按钮上方

and write your doSomething function 并编写doSomething函数

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

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