jQuery Mobile 和表单提交
我决定跳入 jQuery Mobile 框架来实现支持 Wordpress 的移动主题。
我现在遇到了提交带有 url 中的哈希标签的表单并尝试进行验证和 ajax 发布的问题。基本上是行不通的。
例如:website.com/contact/ <- 有效 website.com/#/contact/ <- 不起作用
我知道 href 的 rel="external" 标签可以消除 url 中的 #。但我有一些带有自定义插件的博客文章,该插件会呈现我无法使用 rel="external" 的注册表单。我想我可以将它用于所有链接,但这会消除平滑过渡。
我可以采取哪些选择来尝试使其发挥作用?我正在尝试将 .submit 绑定到表单,进行一些验证,然后 ajax 发布它。
更新 -
<form id="myform" action="myfile.php" method="post">
<input type="text" id="mytext" name="mytext" />
<input type="submit" id="myform_submit" value="Submit">
</form>
和我的脚本:
jQuery(document).ready(function() {
jQuery("#contact_submit").submit(function(){
alert('WTF');
});
});
将其更改为:
<form id="myform" action="myfile.php" method="post">
<input type="text" id="mytext" name="mytext" />
<input type="button" id="myform_submit" value="Submit">
</form>
和我的脚本:
jQuery(document).ready(function() {
jQuery("#contact_submit").click(function(){
alert('WTF');
});
});
两者都不适用于网址中的 #。
我还在 jquery.mobile.js 文件之前添加了这个:
<script type="text/javascript">
jQuery(document).bind(
"mobileinit", function(){
jQuery.extend( jQuery.mobile, { ajaxFormsEnabled: false });
});
</script>
仍然不行。
(仅供参考,jQuery 而不是 $ 是因为 WordPress)
——另一个更新。
由于我使用的是 WordPress,所以有些功能表现得很奇怪。就像 is_home() 一样。无论我在哪个“页面”,该函数都会返回 true。我认为这与每个页面的 ajax 调用有关。
I have decided to jump into the jQuery Mobile framework for a Wordpress enabled mobile theme.
I am now running into the issue of submitting forms with the hash tag in the url and trying to do validation and ajax posting. Basically it does not work.
ex: website.com/contact/ <- works
website.com/#/contact/ <- does not work
I am aware of the rel="external" tag for href's which eliminate the # from the url. But I have blog posts with a custom plugin that renders sign up forms that I will not be able to use the rel="external" for. I guess I could use it for all links but that would eliminate the smooth transitions.
What are my options to try and get this to work? I am trying to bind the .submit to the form, do some validation and then ajax post it.
Update--
<form id="myform" action="myfile.php" method="post">
<input type="text" id="mytext" name="mytext" />
<input type="submit" id="myform_submit" value="Submit">
</form>
and my script:
jQuery(document).ready(function() {
jQuery("#contact_submit").submit(function(){
alert('WTF');
});
});
Changed that to:
<form id="myform" action="myfile.php" method="post">
<input type="text" id="mytext" name="mytext" />
<input type="button" id="myform_submit" value="Submit">
</form>
and my script:
jQuery(document).ready(function() {
jQuery("#contact_submit").click(function(){
alert('WTF');
});
});
Both do not work with the # in the url.
I also added this BEFORE the jquery.mobile.js file:
<script type="text/javascript">
jQuery(document).bind(
"mobileinit", function(){
jQuery.extend( jQuery.mobile, { ajaxFormsEnabled: false });
});
</script>
Still no go.
(FYI the jQuery instead of $ is because of WordPress)
--another update.
Since I am using Wordpress some of the functions are acting weird. Like is_home(). No matter what 'page' I am on the function comes back as true. I think this has to do with the ajax calls for each page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定您在哪里调用
jQuery.ready
函数,但注册处理程序的推荐方法是绑定到特定页面的pagecreate
事件。pagecreate
事件应仅由首页中的jQuery.ready
处理。我最近使用 jQuery mobile 构建了一个网站,并且遇到了这个问题。以下代码应该可以让您到达您想要的位置:不幸的是,我没有进行足够的实验来确定所有这些是否都是必要的。我确实知道这对我有用。我几乎本能地将
return false
添加到由 AJAX 处理的所有表单提交中。希望这有帮助!I'm not entirely sure where you are calling your
jQuery.ready
function but the recommended way to register handlers is by binding to thepagecreate
event for your specific pages. Thepagecreate
events should be handled byjQuery.ready
in the first page only. I recently built a site using jQuery mobile and I ran into exactly this problem. The following code should get you where you want to be:Unfortunately, I didn't experiment enough to determine if all of that is necessary. I do know that this did the trick for me. I almost instincively add the
return false
to all of my form submits that are handled by AJAX. Hope this help!您可以关闭 AJAX 包装。阅读此处: http://jquerymobile.com/demos/1.0a3/# docs/api/globalconfig.html
另外 - 我见过一些关于斜线问题的内容,但现在我找不到它,所以一定要使用 alpha3 版本的 JQM
[编辑]
这被提到了几次之前 - 在其他一些线程中。如果您转到一个页面并且 JQM 使用 AJAX 加载它,则仅采用
body
并且不会触发 document.ready,因为 dom 已经准备好;)(我在这里引用自己的话)You can switch off AJAX wrapping. Read here: http://jquerymobile.com/demos/1.0a3/#docs/api/globalconfig.html
Also - I've seen something about problems with slashes, but now I can't find it, so be sure to use alpha3 version of JQM
[edit]
This was mentioned a few times before - in some other threads. If you go to a page and JQM loads it with AJAX, then only
body
is taken AND NO document.ready fires, as the dom is ready already ;) (I'm quoting myself here)