更新:我的电子邮件表单是id,但仍有同样的问题。
我有一个表单,当我提交时,应该弹出并显示一个颜色框,但代码打开颜色框,但在眨眼间,页面刷新?
这是我的代码:
<html data-wf-page="546cd4ede4d" data-wf-site="5d0115c4ca7148">
<head>
<meta charset="utf-8">
<title>Request Form</title>
<meta content="Request a now." name="description">
<meta content="Request Thingy" property="og:title">
<meta content="Request a thingy now." property="og:description">
<meta content="summary" name="twitter:card">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="Webflow" name="generator">
<link href="css/normalize.css" rel="stylesheet" type="text/css">
<link href="css/webflow.css" rel="stylesheet" type="text/css">
<link href="css/sdfj.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script>
<script type="text/javascript">
WebFont.load({
google: {
families: ["Ubuntu:300,300italic,400,400italic,500,500italic,700,700italic","Varela Round:400","PT Sans:400,400italic,700,700italic"]
}
});
</script>
<script src="js/modernizr.js" type="text/javascript"></script>
<link href="images/favicon2.png" rel="shortcut icon" type="image/x-icon">
<link href="images/myfav.png" rel="apple-touch-icon">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-526-3'], ['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://thingyss.com/css/colorbox.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style>
#wrapper {
position: relative;
display: inline-block;
width: 100%;
z-index: 1;
}
#wrapper input {
padding-right: 14px;
}
#wrapper:after {
content: "*";
position: absolute;
right: 15px;
top: +12px;
/*color: #ed9900;*/
color: #39a2e2;
z-index: 5;
}
</style>
</head>
<body class="requestbody">
<form id="email-form" name="email-form">
..........
<div class="form_half_div right">
<div class="centered">
<input class="rounded_btn w-button w-preserve-3d" type="submit" value="Request Now" data-wait="Finding..." wait="Finding...">
</div>
</div>
</form>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBpz8DQg&v=3.exp&libraries=places">
</script>
<script src="js/socket.io.min.js"></script>
<script type="text/javascript" src="js/mystuff.js"></script>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script src="js/autoNumeric.js"></script>
<script src="js/jquery.colorbox-min.js"></script>
<script>
$(document).ready(function() {
setTypes();
initialize();
$("#email-form").submit(function(){
requestTruckWarning(); // creates a colorbox
e.preventDefault(); // trying to prevent page from refreshing but never hits this breakpoint?
return false;
});
document.onkeypress = stopReturnKey;
});
function initialize() {
var options = {
types: ['(cities)'],
componentRestrictions: {country: "us"}
};
var mapOptions = {
center: { lat: 39.707187, lng: -100.722656},
zoom: 14,
draggable: false,
scrollwheel: false
};
var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
google.maps.event.addListener(map, 'click', function() {
map.setOptions({draggable: true});
});
bootstrapHome(map);
}
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
autocomplete.setBounds(new google.maps.LatLngBounds(geolocation,
geolocation));
});
}
}
</script>
<!-- <script src="js/jquery-2.1.1.js"></script> -->
<script src="js/main.js"></script>
<!-- [if lte IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/placeholders/3.0.2/placeholders.min.js"></script><![endif] -->
</body>
</html>
答案 0 :(得分:7)
您忘记将事件变量e
作为参数传递给提交回调:
$("#email-form").submit(function(e){ //Here
requestTruckWarning(); // creates a colorbox
e.preventDefault(); // trying to prevent page from refreshing but never hits this breakpoint?
});
答案 1 :(得分:3)
在你的回调函数中,你错过了传递的参数e
(事件对象):
$('#email-form').submit(function (e) {
requestTruckWarning() // Creates a colorbox
e.preventDefault() // This now works, because `e` exists
})
此外,它看起来像其他一些答案(归功于Jai)指出#email-form
没有引用文档中的元素。必须在上述代码成功运行之前修复。
答案 2 :(得分:2)
您的form
没有任何名为#email-form
的ID。添加ID属性:
<form id='#email-form'>
或更改选择器:
$('form').submit(function(){
并且还提到了其他答案,您必须在提交回调中将e
作为事件传递。
$('form').submit(function(e){ // <-------here
然而,其他两项变更必须按照以上建议进行。
答案 3 :(得分:1)
这里实际上有两个问题。正如@Jai所说,你没有为表单设置id属性,所以jQuery正在寻找id ='email-form'的元素,什么都没找到,所以代码不会执行。即使它确实执行了,它也不会停止表单提交,因为当你使用e.preventDefault时,在这种情况下'e'是事件对象,这是你需要传递给事件的事情处理程序。将id添加到for,然后更改事件处理程序的声明,如下所示:
$("#email-form").submit(function(e){