更改表单中提交的值和JQuery序列化函数 [英] Change values in form submitted and the JQuery serialize function

查看:90
本文介绍了更改表单中提交的值和JQuery序列化函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码。当表单被提交时,它是否实际上提交了我输入的值,即val(50),或者在serialzies它只是从实际的html页面上的表单获取数据?

  //停止所有表单提交并提交真实(隐藏)表单#order 
$('form:not(#order)')。submit函数(事件){
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'); $ b'b
$('form#order input [name = productname]')。val(name);

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

});
} else {
//
}

我想要那些无论用户设置了什么值,我都会正确地设置这些值?



感谢所有人

解决方案



<$>为什么不直接构造数据而不是将其填充到表单中,然后通过序列化获取值? ($(this).attr('id')==='quick2a'){$($))$($) b $ b 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 {
//
}
re转为假;
});


Please look at the following code. When the form gets submitted, is it actually submitting the values I have entered i.e. val(50) or at the point it serialzies does it just get data from the form on the actual html page?

// 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;
});

这篇关于更改表单中提交的值和JQuery序列化函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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