jQuery Mobile 和表单提交

发布于 2024-10-16 10:32:55 字数 1608 浏览 1 评论 0原文

我决定跳入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-10-23 10:32:55

我不完全确定您在哪里调用 jQuery.ready 函数,但注册处理程序的推荐方法是绑定到特定页面的 pagecreate 事件。 pagecreate 事件应仅由首页中的 jQuery.ready 处理。我最近使用 jQuery mobile 构建了一个网站,并且遇到了这个问题。以下代码应该可以让您到达您想要的位置:

jQuery(document).ready(function() {
    $("#id_of_page").live('pagecreate', function() {
        $("form").submit(function(event) {
            event.stopPropagation();
            event.preventDefault();

            // Do stuff here (usually AJAX);

            return false;
        });
    });
});

不幸的是,我没有进行足够的实验来确定所有这些是否都是必要的。我确实知道这对我有用。我几乎本能地将 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 the pagecreate event for your specific pages. The pagecreate events should be handled by jQuery.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:

jQuery(document).ready(function() {
    $("#id_of_page").live('pagecreate', function() {
        $("form").submit(function(event) {
            event.stopPropagation();
            event.preventDefault();

            // Do stuff here (usually AJAX);

            return false;
        });
    });
});

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!

孤寂小茶 2024-10-23 10:32:55

您可以关闭 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文