【发布时间】:2017-12-11 04:44:54
【问题描述】:
我正在尝试根据访问者位置重定向按钮单击,并且我有两个窗口的模式。以及使用 freegeoip.net: 我将它与 Wordpress 一起使用:脚本正确连接到 wordpress 并运行,但重定向不起作用。
我同样希望此模式在每次访问时仅出现一次,并且在通过网站的所有导航中出现,而不是在每次页面重新加载时出现。提前致谢。
下面是 jQuery:
jQuery(document).ready(function(){
jQuery("#myModal").modal('show');
jQuery.ajax({
url: 'https://freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
// If the visitor is browsing from Canada.
if (location.country_code === 'CA') {
jQuery('#subscriber-ca').on('click', function(){
window.location.replace('http://www.google.com');
});
jQuery('#subscriber-us').on('click', function(){
// Redirect visitor to the USA.
window.location.replace('http://www.facebook.com');
});
} else if(location.country_code === 'US') {
jQuery('#subscriber-ca').on('click', function(){
window.location.replace('http://www.google.com');
});
jQuery('#subscriber-us').on('click', function(){
// Redirect visitor to the USA.
window.location.replace('http://www.facebook.com');
});
}
}
});
});
这是模态:
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Subscribe our Newsletter</h4>
</div>
<div class="modal-body">
<p>Subscribe to our mailing list to get the latest updates straight in your inbox.</p>
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email Address">
</div>
<button id="subscribe-us" type="submit" class="btn btn-primary pull-left">US Subscriber</button>
<button id="subscribe-ca" type="submit" class="btn btn-primary pull-right">Canada Subscriber</button>
</form>
</div>
</div>
</div>
</div>
【问题讨论】:
-
当您尝试单击重定向但失败时,控制台中有什么内容?
-
我试图控制台记录点击事件,但没有反馈:这就是我在控制台上能找到的全部内容,JQMIGRATE:已安装 Migrate,版本 1.4.1
-
为什么要使用点击成功功能?
-
你想触发点击事件还是用户想点击?
-
@vel 是的,我想要一个点击事件,当访问者点击按钮时,他/她应该根据他/她的位置和点击的按钮进行重定向,因为模式有两个按钮。我不确定这里缺少什么。因为即使使用 console.log 也不会在控制台上打印任何内容:(
标签: javascript jquery wordpress freegeoip