今天在看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"> <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}的形式,還有這樣的一種 強大的資料處理方式。
希望大家都好好用用!