简体   繁体   English

更改表单提交的值和JQuery序列化功能

[英]Change values in form submitted and the JQuery serialize function

Please look at the following code. 请看下面的代码。 When the form gets submitted, is it actually submitting the values I have entered ie val(50) or at the point it serialzies does it just get data from the form on the actual html page? 提交表单后,它实际上是在提交我输入的值val(50)还是在序列化时从实际html页面上的表单中获取数据?

// stop all forms from submitting and submit the real (hidden) form#order
$('form:not(#order)').submit(function(event) {
alert($(this).attr('id'));                                
//event.preventDefault();
if($(this).attr('id')==='quick2a'){

    alert('quick2a being submitted');
    //submitQuick2a();
    $('form#order input[name=custom_channels]').val(50);

    var name = 'het-';
    name += $('form#order input[name=platform]').val('astsk');
    name += '-ga-';
    name += $('form#order input[name=license]').val('floating');

    $('form#order input[name=productname]').val(name);

    $.post('/store/cart/add/ajax/', $('form#order').serialize(), function() {
    document.location.href = '/store/checkout';

    });                 
}else{
//
}

I want those values to be set in the form regardless of what is set by the user, am I doing this correctly? 无论用户设置了什么,我都希望以表格形式设置这些值,我这样做是否正确?

Thanks all 谢谢大家

Why not just construct the data directly instead of stuffing it into a form and then grabbing the values via serialize? 为什么不直接构建数据而不是将其填充为表单然后通过序列化获取值呢?

$('form').submit(function(event) {
    if($(this).attr('id')==='quick2a') {
        var data = {
                 'custom_channels': 50,
                 'platform' : 'astsk',
                 'license' : 'floating',
                 'productname' : 'het-astsk-ga-floating'
            };

        $.post('/store/cart/add/ajax/', data, function() {
            document.location.href = '/store/checkout';
        });                                 
    }else{
    //
    }
    return false;
});

它从HTML页面获取值...但是通过调用.val(...)您可以在HTML页面上设置值,因此您的代码将按您希望的那样工作。

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

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