简体   繁体   中英

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();" top your button

and write your doSomething function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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