JQuery的阿贾克斯形式和动态创建的表单元素不提交 [英] JQuery Ajax Form and Dynamically created form elements not submitting

查看:122
本文介绍了JQuery的阿贾克斯形式和动态创建的表单元素不提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写有动态加载一些文本输入的元素,当一个选择列表被改变的形式。

I'm writing a form that has some text input elements loaded dynamically when a select list is changed.

现在的问题是,当我提交表单的元素没有被发送到服务器发送的数据。

The problem is that when I submit the form those elements are not sent in the data that is posted to the server.

什么我需要做的就是这些动态创建的元素成的形式提交?

What do I need to do to get those dynamically created elements "into" the form to be submitted?

在code是这样的:

$("#my_select_id").change(function() {
    $.ajax({
        type: "GET",
        url: "some-url/" + $("#played_number_game_id").children("option:selected").val(),
        async: false, 
        dataType: "html",
        error: function(XMLHttpRequest, status, errorThrown) {
            alert("oh no!");
            alert(status);
        },
        success: function (data, status) {
            $("#parent-element").html("");
            $("#parent-element").append(data);
        },
        complete: function() {
        }
    });

    $("#my_form_submit").click(function() {
        $("#my-form").ajaxSubmit({ clearForm: true }); 
        return false;
    });
});

在Ajax调用返回的HTML是:

The html returned by the ajax call is:

<input id="my-form_e_1" class="number-input" type="text"/>
<input id="my-form_e_2" class="number-input" type="text"/>

如果我使用Firebug调用AJAX方法后看页面的动态加载HTML是正确的层次结构中,它应该是...即它的形式。

And if I use firebug to look at the page after calling the ajax method the dynamically loaded html is right in the hierarchy where it should be...i.e. it's in the form.

但是,当我点击提交按钮存在的AJAX调用get之前发布形式的唯一元素。

But when I click the submit button only the elements of the form that existed before the ajax call get posted.

任何想法?

推荐答案

您的问题似乎是动态添加的输入元素没有名称属性。输入单元要求名称如果你希望自己的价值观被纳入提交的数据属性。

Your problem seems to be that the dynamically added input elements do not have name attributes. Input elements require name attributes if you want their values to be included in the submitted data.

这篇关于JQuery的阿贾克斯形式和动态创建的表单元素不提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆