简体   繁体   中英

if using jquery to append multiple fields they are not posted to array

Basically, I have page, where is static html, code:

<input type="text" name="exp[][from]" />
<input type="text" name="exp[][to]" />
<input type="text" name="exp[][position]" />
<input type="text" name="exp[][name]" />

After this input fields there is button to add more fields, that runs this jQuery code:

    $(document).ready(function(){
    $('#addwork').click(function() {
        $(document.createElement('div')).html('<input type="text" name="exp[][from]" />\n<input type="text" name="exp[][to]" />\n<input type="text" name="exp[][position]"  />\n<input type="text" name="exp[][name]" />').appendTo('.cvs');
    });
});

Problem is that if I add some fields with jQuery they are not posted to array( tested with var_dump ), but static one is. I tested and added few more fields in static html and it worked well.

So basically, if i append input fields they are not posted to array, but static ones are.

Well I tried with a sample code and I am able to get the values of the appended items using var_dump. So please check this code

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
   $(document).ready(function(){
    $('#addwork').click(function() {
        $(document.createElement('div')).html('<input type="text" name="exp[][from]" />\n<input type="text" name="exp[][to]" />\n<input type="text" name="exp[][position]"  />\n<input type="text" name="exp[][name]" />').appendTo('.cvs');
    });
});

</script>
</head>
<form method="post">
<input type="text" name="exp[][from]" />
<input type="text" name="exp[][to]" />
<input type="text" name="exp[][position]" />
<input type="text" name="exp[][name]" />
<div class="cvs"></div>
<input type="submit" value="btn" name="btn"/>

<input type="button" value="add" id="addwork"/>
</form>
</html>
$(document).ready(function() {
                    $('#addwork').click(function() {
                        $('.cvs').append(
                                '<div>'+
                                    '<input type="text" name="exp[][from]" /><br>'+
                                    '<input type="text" name="exp[][to]" /><br>'+
                                    '<input type="text" name="exp[][position]"  /><br>'+
                                    '<input type="text" name="exp[][name]" />'+
                                '</div>');
                        });
                });

通过这种方式将名称属性的格式设置为exp [INTEGER] [info],您可以将表单数据“分组”,但可能导致大数组

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