使用AJAX提交两个值

问题描述 投票:1回答:2

我想用这个jQuery代码提交/发布两个值:

$('input#code1').on('input', function(){
  var name = $('input#code1').val();
  if ($.trim(name) != ''){
    $.post('ajax/rate1.php', {code1: name}, function(data){
      $('div#total1').text(data);
    });
  }
});

我怎样才能包含一个附加值(qty1)?

javascript jquery ajax
2个回答
2
投票

假设你在qazxsw poi中有qty只是检索值并添加到javascript对象的帖子

$('input#code2')

0
投票

jQuery的 var name = $('input#code1').val(); var qty = $('input#code2').val(); if ($.trim(name) != ''){ $.post('ajax/rate1.php', {code1: name, code2, qty}, function(data){ $('div#total1').text(data); }); } 接受一个数据对象,它可以包含多个值。

数据 键入:PlainObject或String 与请求一起发送到服务器的普通对象或字符串。

例如:

$.post()

然后,在PHP中:

var parameters = {
  "name" : $('input#code1').val(),
  "qty"  : $('input#qty1').val()
};

$.post('ajax/rate1.php', parameters, function(data){
  $('div#total1').text(data);
});

供参考,请参阅$name = !empty($_POST['name']) && !ctype_space($_POST['name']) ? trim($_POST['name']) : '(unnamed)'; $qty = !empty($_POST['qty']) && is_numeric($_POST['qty']) ? $_POST['qty'] : 0;

examples in the jQuery documentation
© www.soinside.com 2019 - 2024. All rights reserved.