我試圖用ajax將點擊按鈕的值發送到我的php文件。即用戶點擊btn1
,然後值1
應該推入陣列和阿賈克斯應該發送到我的文件screen.php
。推送陣列中單擊元素的值
我現在控制檯顯示Yay it worked
我的JS:
jQuery(document).ready(function($){
var ajaxurl = 'screen.php';
$values = [];
$('.ui-btn').click(function(){
$values.push($(this).val());
});
$.ajax({
url: ajaxurl,
type: 'post',
data: {
buttons: $values,
},
success: function (result) {
console.log('Yay it worked' + $values);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log('Something went wrong');
}
});
});
我的html:
<div>
<button name="btn1" type="submit" id="btn1" value="1" class="ui-btn"></button>
<button name="btn2" type="submit" id="btn2" value="2" class="ui-btn"></button>
</div> // I have up to 9 buttons, thats only a piece
'$ values'在範圍中不可用? – hjpotter92
你的問題是什麼? – Teemu
您的'.ajax'似乎在點擊之前被調用,甚至比domready早。 – fuyushimoya