我试图通过jquery将按钮的某些值输入到php中。我是这样做过的。
HTML
<button class="button" name="reject" value="<?php echo $row['mail']; ?>" type="submit">reject</button>
Jquery的
$(document).ready(function(){
$('.button').click(function(){
var url = window.location.href;
var whichBtn = $(this).attr("name");
var mail = $(this).attr("value");
var ajaxurl = url,
data = {'mail': mail, 'whichBtn' : whichBtn};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert(whichBtn);
alert("action performed successfully");
});
});
});
PHP
if(isset($_POST['mail']))
echo $_POST['mail'];
嗯,问题是POST[mail]
没有设定,我也不知道为什么......你能帮忙吗?
答案 0 :(得分:1)
您需要将event.preventDefault()
添加到点击处理程序中。由于它是一个提交按钮,因此在您执行Javascript之前,它会离开页面。试试这个:
$(document).ready(function(){
$('.button').click(function(event){
event.preventDefault();
var url = window.location.href;
var whichBtn = $(this).attr("name");
var mail = $(this).attr("value");
var ajaxurl = url,
data = {'mail': mail, 'whichBtn' : whichBtn};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert(whichBtn);
alert("action performed successfully");
});
});
});