dtpf76658
2015-05-08 22:11
浏览 140

发送带有serialize()函数的表单和ajax()中的额外数据

I'm programming a web page for a company in CI3, and I've another problem. I need to send to wizard.php(controller)a serialized form and a javascript variable, but, I think that I can't send 2 variables at the same time, here is my code.

var idCompany =5;
$(document).on('submit', '#period-form', function(event) {
    event.preventDefault();
    $.ajax({
        url: '<?php echo base_url()?>wizard/insertPeriod/'
        type: 'POST',
        dataType: 'json',
        data: $("#period-form").serialize(),
        success : function (json) {
            $("#alert").append(json.response_alert);
        },
        error : function (xhre) {
            console.log(xhre);
        }
    })
});

As you can see, idCompany is an attribute, it has value but, I put value in another function. is possible add values to a serialized form? or how I can send a serialized form and a variable at the same time?

图片转代码服务由CSDN问答提供 功能建议

我正在为CI3中的公司编写网页,我还有另外一个问题。 我需要 发送给wizard.php(控制器)一个序列化的表单和一个javascript变量,但是,我认为我不能同时发送2个变量,这是我的代码。

 <  code> var idCompany = 5; 
 $(document).on('submit','#period-form',function(event){
 event.preventDefault(); 
 $ .ajax({
 url  :'&lt;?php echo base_url()?&gt; wizard / insertPeriod /'
 type:'POST',
 dataType:'json',
 data:$(“#period-form”)。serialize(  ),
 success:function(json){
 $(“#alert”)。append(json.response_alert); 
},
 error:function(xhre){
 console.log(xhre);  
} 
})
}); 
   
 
 

正如您所看到的,idCompany是一个属性,它有值但我把值放在另一个函数中 。 是否可以为序列化表单添加值? 或者我如何同时发送序列化表格和变量?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjie1339 2015-05-08 22:14
    已采纳

    The serialize function simply creates an URL query string from all the form elements. So you can manually add the variable to the result of the serialize() function like this:

     data: $("#period-form").serialize() + '&idCompany=' + idCompany
    
    点赞 打赏 评论
  • dongwo5110 2015-05-08 22:16
    var data = $('#period-form').serializeArray();
    data.push({ name: "<somename>", value: "<somevalue>" });
    

    You can use push method to add more name - value pairs.

    点赞 打赏 评论

相关推荐