我一直在看Youtube上的这个教程!,它就是不想按我想要的方式工作。
我得到了这段代码在我的index.php:
<form action="scripts/keys.php" class="ajax" method="post">
<p>Key:</p>
<input type="text" name="key" class="key_input" autocomplete="off" autofocus />
<input type="submit">
</form>
main.js中的代码:
$('form.ajax').on('submit', function(){
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[key]').each(function(index, value){
var that = $(this),
name = that.attr(''),
value that.val('');
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
console.log(response);
}
});
return false;
});
最后是my keys.php:
if(isset($_POST['key'])) {
echo 'lol';
}
由于某些原因,当我提交表单时,它仍然将我发送到keys.php,我不明白,当我使用return false时。
有人能解释一下我做错了什么吗?不要告诉我正确的代码,只是我需要改变的:)语法错误,缺少等号
that.find('[key]').each(function(index, value){
var that = $(this),
name = that.attr(''),
value that.val(''); // HERE
// ^^
data[name] = value;
});
并将PHP改为
if(isset($_POST['key'])) {
echo 'lol';
}