简体   繁体   English

Ajax序列化父子窗体

[英]Ajax Serialize parent child form

Im currently using ajax to save values from my form and its working well. 我目前正在使用ajax来保存表单中的值及其正常运行。 The problem is when I have parent child forms its no longer working. 问题是当我有亲子形式不再工作时。 Here is my ajax code: 这是我的ajax代码:

function saveChildren_ajax(){
    $.ajax({
        type: "POST",
        url: "addchildren.php",
        data: jQuery("#myform > .childrenSave").serialize(),
        cache: false,
        success:  function(data){
            //do anything   
        }
    });  
} 

And my html code: 而我的html代码:

<form id="myform" >
  <form class="childrenSave">

    <input type="text" class="form-control" name="i_child" id="i_child">
    Gender:

    <input type="radio" name="i_optgender" value="Male" checked>Male
    <input type="radio" name="i_optgender" value="Female">Female

    <input type="number" class="form-control" name="i_age" id="i_age">
    <button type="button" onclick="saveChildren_ajax()">  Add another child  </button>
  </form>
</form>

Please help me how to get the value from a parent child Forms Thanks! 请帮助我如何从亲子表格中获取价值。表格谢谢!

You can't use nested forms in HTML. 您不能在HTML中使用嵌套表单。 It is specified in HTML specification. 在HTML规范中指定。 You can find more about this topic here: Can you nest html forms? 您可以在此处找到有关此主题的更多信息: 可以嵌套html表单吗?

See this corrected code. 请参阅此更正的代码。

 $(function() { $('#myform').on('submit', function() { var data = $(this).serialize(); $.ajax({ type: "POST", url: "addchildren.php", data: data, cache: false, success: function(data) { //do anything } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form id="myform"> <input type="text" class="form-control" name="i_child" id="i_child" />Gender: <input type="radio" name="i_optgender" value="Male" checked="checked" />Male <input type="radio" name="i_optgender" value="Female" />Female <input type="number" class="form-control" name="i_age" id="i_age" /> <button type="submit">Add another child</button> </form> 

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

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