简体   繁体   English

AJAX / Javascript获取提交的表单数据

[英]AJAX/Javascript Get Submitted Form Data

In javascript I am creating multiple forms and inputting into html us ajax/js like this: 在javascript中,我正在创建多种形式,然后将ajax / js输入到html中,如下所示:

function addProduct(product) {
                    var html = '';
                    html += '<div>'
                    html += '<form id="product-form">';
                    html += '<div class="city">'+product['product_name']+'</div>'
                    html += '<div class="state">'+product['product_price']+'</div>'
                    html += '<input type="hidden" name="product_id" value="'+product['product_id']+'" id="product_id" >'
                    html += '<a href="#" class="whiteButton submit" id="view-product-button" >View</a>'
                    html += '</form>'
                    html += '</div>'
                    var insert = $(html);
                    $('#main').append(html);
                }

Multiples of these forms are created with different product_ids. 这些表单的倍数是使用不同的product_id创建的。 The problem I am running into is getting the values of just that form. 我遇到的问题是获取该表格的值。 I am able to correctly identify the form that was submitted but I am not sure out to the the product_id from the input. 我能够正确识别已提交的表单,但不确定从输入中获得product_id的信息。 So far I have this: 到目前为止,我有这个:

$('#main').on('submit', '#product-form', function() {

                    var product_id = $('?????').val()

                });

How can I get the product id and other related data from the form that was submitted? 如何从提交的表单中获取产品ID和其他相关数据?

Use class instead of id for forms and use submit button instead of "a tag", then make your submit function like this 在表格中使用class而不是id,并使用Submit按钮而不是“ a tag”,然后使您的Submit函数像这样

$('.form_class').on('submit', function(e){
    e.preventDefault();
    var form = $(this);
    var formUrl=form.attr('action');
    var product_id = $('#product_id', form).val();

    $.ajax({
        type: "POST",
        url: formUrl,
        data: product_id, // or use form.serialize();
        success: function(data){
            // code goes here;
        },
        error:function (xhr, ajaxOptions, thrownError){
            // code goes here;
        }
    });
});

You can provide a context to the jQuery $() function: 您可以为jQuery $()函数提供上下文:

var product_id = $('input[name=product_id]', this).val();

The 'input...' selector will apply only within the context of the second parameter, in this case this , where this will be set by jQuery to be the current form element within the the submit handler. 'input...'选择器将只应用内的第二参数的情况下,在这种情况下this ,在this将通过jQuery的被设置为在所述内的当前形式元件submit处理程序。

Alternatively you can select the current form first and then .find() elements within it: 或者,您可以先选择当前表单,然后选择其中的.find()元素:

var product_id = $(this).find('input[name=product_id]').val();

Note that I'm selecting the input by name because although you've given that element an id attribute it is not valid to assign the same id to multiple elements. 请注意,我要按名称选择输入,因为尽管您已为该元素提供了id属性,但将相同的id分配给多个元素是无效的。

$(this).find('.city').text();
\n

The short answer is: 简短的答案是:

$('#main').on('submit', '#product-form', function() {
    var product_id = $(this).children('input[name=product_id]').val();
});

But I can see a problem with your function. 但是我可以看到您的功能有问题。 In fact, calling addProduct(...) multiple times will end up invalidating your DOM, since you will have more elements with the same id ( product-form , product_id and view-product-button ). 实际上,多次调用addProduct(...)最终会使您的DOM无效,因为您将拥有更多具有相同ID的元素( product-formproduct_idview-product-button )。 Better review the whole idea behind it... 更好地回顾其背后的整个想法...

First of all, you must not create multiple elements with the same id. 首先,您不能创建具有相同ID的多个元素。 Use class. 使用类。 There is a lot of ids in your javascript, but for the Form it'll be : html += '<form class="product-form">'; 您的javascript中有很多ID,但对于Form,它将是: html += '<form class="product-form">'; for the form declaration and $('#main').on('submit', '.product-form', function() { to catch the event. 用于表单声明和$('#main').on('submit', '.product-form', function() {以捕获事件。

Then, when you catch an event, this will refer to the current event target. 然后,当你抓住一个事件, this将涉及当前事件的目标。 You can retrieve the value of the product by using : $('input[name=product_id]', this)[0].value 您可以使用:$('input [name = product_id]',this)[0] .value来检索产品的值。

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

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