1

我在发布数据属性的值时遇到了麻烦,例如 data-fruit="orange" 我基本上想用 jQuery ajax 来做,我想它看起来像这样

var fruit = $(".fruit img").attr("data-skin");

$('.box').click(function() {
  $.ajax({
    url: 'fruits.php',
    type: 'post',
    data: fruit
  });
});

php $_POST 应该像这样:

$friutType = $_POST['data-friut']

干杯

4

1 回答 1

4

您需要将键/值对发送到data,而不仅仅是一个值。

$.ajax({
    url: 'fruits.php',
    type: 'post',
    data: {fruit: fruit}
});

然后在 PHP 中:

$friutType = $_POST['fruit']
于 2012-07-05T18:24:55.190 回答