Rails:Javascript e.preventDefault();工作不正常
我已经尝试解决这个问题很长一段时间了,但到目前为止没有任何运气。因此,任何帮助都将不胜感激 我在dynamics选项卡中有两个搜索Rails:Javascript e.preventDefault();工作不正常,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我已经尝试解决这个问题很长一段时间了,但到目前为止没有任何运气。因此,任何帮助都将不胜感激 我在dynamics选项卡中有两个搜索form_标签: <div id="tabs"> <ul class="nav nav-tabs" id="prodTabs"> <li class="active"><a href="#search">Search</a></li> <li><a href=
form_标签
:
<div id="tabs">
<ul class="nav nav-tabs" id="prodTabs">
<li class="active"><a href="#search">Search</a></li>
<li><a href="#other" data-toggle="tab">Other</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nearby">
<%= render 'pages/search' %>
</div>
<div class="tab-pane" id="other">
<%= render 'pages/search_other' %>
</div>
<script>
$('#tabs').on('click','.tablink,#prodTabs a',function (e) {
e.preventDefault();
var url = $(this).attr("data-url");
if (typeof url !== "undefined") {
var pane = $(this), href = this.hash;
// ajax load from data-url
$(href).load(url,function(result){
pane.tab('show');
});
} else {
$(this).tab('show');
}
});
</script>
search.js
function getGeoLocation() {
navigator.geolocation.getCurrentPosition(setGeoCookie);
}
function setGeoCookie(position) {
var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
document.cookie = "lat_lng=" + escape(cookie_val);
$('form').submit();
}
试着放在控制台上。使用
$('#form').submit(函数(e){console.log(“形式”,e)})代码>,请在提交表格后告诉我您得到了什么谢谢您的回复@Vishal。。。你是说像这样吗<代码>$('#form').submit(函数(e){console.log(“形式”,e)e.preventDefault();getGeoLocation();})代码>首先检查表单提交是否正确,然后再转到函数。当前,仅使用此$('#form')对另一个函数ok进行注释代码>它确实提交了正确的表单并正确重定向!!!控制台或web浏览器检查器中没有错误!您是否获得e
的值?
<div id="other">
<%= form_tag url_for(:controller => 'pages', :action => 'search_other'), method: :get do %>
<%= text_field_tag :search_other, params[:search_other], autofocus: true, placeholder: "Enter keyword to search" %>
<%= submit_tag "Search", :style => "display: none;" %>
<%end%>
</div>
<script>
$('#form').submit(function(e) {
e.preventDefault();
getGeoLocation();
});
</script>
function getGeoLocation() {
navigator.geolocation.getCurrentPosition(setGeoCookie);
}
function setGeoCookie(position) {
var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
document.cookie = "lat_lng=" + escape(cookie_val);
$('form').submit();
}