HTML --
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
Field: <span id="test"></span>
<form id="form" method="post">
<input type="submit" name="submit" value="Submit" />
<input type="submit" name="submit" value="Don't Click" />
</form>
<script>
/* attach a submit handler to the form */
$("#form").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* set all the vars you want to post on here */
var parameters = {
'submit': $('input[name="submit"]').val()
};
$.ajax({
url: 'php/test.php',
method:'POST',
data: parameters,
success: function(msg) {
$('#test').append(msg);
}
})
});
php --
<?php
$submit = $_POST['submit'];
if($submit === "Submit"){
echo 'Success!';
} else if($submit === "Don't Click") {
echo 'You Effed Up!';
}
?>
当我运行 Ajax 调用时,它总是在 Field: 区域中显示 Success。这是为什么?它不应该获取单击的提交按钮的值吗?