我正在尝试在不刷新页面的情况下提交表单,但event.preventDefault();
无法正常工作。这是我到目前为止所拥有的
$('#contactform').on('submit', function() {
event.preventDefault();
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
succss: function(response) {
console.log(response);
}
});
});
但是一旦按下提交按钮,页面仍然会重新加载。有什么建议么?
更新:主表单页面的代码如下;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="<?php echo get_template_directory_uri();?>/ajax/main.js"></script>
<form action="<?php echo get_template_directory_uri(); ?>/ajax/contact.php" method="post" id="contactform">
<input type="text" name="fname" placeholder="Name" />
<input type="email" name="email" placeholder="Email" />
<textarea name="message" id="" cols="30" rows="10" placeholder="Your Message"></textarea>
<input type="submit" name="Submit" />
</form>