简体   繁体   中英

Load Form Element Values Dynamically

Is it possible to get dynamic values in a form with jQuery?

For example I have this:

<form id="contact" method="post" action"contact.php" class="ajaxForm">
    <label>Name:</label>
    <input type="text" name="name" />
    <label>Email:</label>     
    <input type="text" name="email"/>
    <label>Message:</label>
    <textarea name="message"></textarea>
    <input type="submit" value="send!"/>
</form>

In js:

$(".ajaxForm").send(function(){
    var page = $(this).attr("action");
});

I want to get var name=value with some loop like .each but the code is only reading inputs/textarea/select data.

Thanks for the help!

You can obtain input/textarea/select values in a form with jQuery.

$(".ajaxForm").send(function(){
    var page = $(this).attr("action");
    var its = $(this).find('input, textarea, select');
    its.each(function(){
         console.log($(this).val());
    })
});

Use .children()

$(".ajaxForm").send(function(){
    var page = $(this).attr("action");
    $(this).children('input').each(function() { // Loop over all  the children
                                                // that are input elements
           console.log( $(this).val() );
                       OR

           console.log( this.value );
    });

});

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