ajax post data数据传值分析

前端之家收集整理的这篇文章主要介绍了ajax post data数据传值分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天在看opencart的代码的时候,看到一个很奇特的写法:

$.ajax({
url: 'index.PHP?route=checkout/payment_address/validate',
type: 'post',
data: $('#payment-address input[type=\'text\'],#payment-address input[type=\'password\'],#payment-address input[type=\'checkBox\']:checked,#payment-address input[type=\'radio\']:checked,#payment-address input[type=\'hidden\'],#payment-address select'),

dataType: 'json',
beforeSend: function() {
$('#button-payment-address').prop('disabled',true);
$('#button-payment-address').after('<span class="wait">&nbsp;<img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
},
complete: function() {
$('#button-payment-address').prop('disabled',false);
$('.wait').remove();
},
success: function(json) {
$('.warning,.error').remove();


...此处略去代码 n行


data传递参数,这个包括了所有当前页面input type=text域的值,input type=password域的值,input type=checkBox 选中域的值 以及radio,hidden,select域的值都会传递过去,在url 处理页面 'index.PHP?route=checkout/payment_address/validate' ,可以这样接收 来自html页面ajax传过来的值:

$this->request->post['payment_address'],其中payment_address就是 html页面里面的 <input type="radio" name="payment_address" value="existing" id="payment-address-existing" checked="checked" /> 的。

所以data传参数的形式除了{key:value}的形式,还有这样的一种 强大的数据处理方式。

希望大家都好好用用!

原文链接:https://www.f2er.com/ajax/163422.html

猜你在找的Ajax相关文章