【问题标题】:Jquery validations not working without page refresh in jquery mobile在jquery mobile中没有页面刷新的情况下,jquery验证不起作用
【发布时间】:2013-05-29 06:29:01
【问题描述】:

我正在使用 jquery validate.js 在我的 jquery 移动应用程序中验证我的联系表单。 当我通过 url 加载页面或页面刷新时,验证似乎工作得很好。 但是当我从另一个刷新的页面链接返回联系表单时,验证将停止工作。

刷新页面时它可以工作。 我尝试了以下方法:

1) 包括 jquery mobile js 之前的contact.js。

2)data-ajax="false";

3) 使用bind

$(document).bind("mobileinit", function(){
      $.extend(  $.mobile , { ajaxEnabled: false });
});

4) refresh form page with jQuery Mobile

5) 使用window.onload

window.onload = function() {
   if(!window.location.hash) {
    window.location = window.location + '#loaded';
    window.location.reload();
 }
}

5)Jquery-Mobile: How to reload/refresh the internal page

6) One time page refresh after first page load Javascript working only on page refresh in jQuery mobile

但是这些都不起作用。我需要做的是在导航到页面时刷新页面一次。 这是我使用 jquery mobile 的联系表单代码。

<form method="post" action="" id="feedback" name="feedback">
     <input type="text" name="user_name" data-role="none" id="name" value="" placeholder= "Your name*" />
     <br />
     <input class="email" required="true" type="text" name="user_email" data-role="none" id="email" value="" placeholder="Your Email*" />
    <br />
    <textarea cols="70" rows="12" class="required" name="user_message" id="c_message" data-role="none" placeholder="Your message*">
   </textarea>
   <br />
   <input type="submit" value="Send" data-mini="true" data-role="none" data-inline="true" class="green_like" id="send" />
</form>

这是位于“联系我们”表单中的 JS 代码。

$(document).bind("pageinit",function(){

  $("#feedback").validate({

    submitHandler : function() {
        $.ajax({
            type:'POST',
            url :'/some/url',
            data: $('#feedback').serialize(),
            success: function(resp) {
                if(resp==1)
                {   
                    $("#success_post_contact").html('<label style="color:green;margin-left: 6px;" >Thank you for contacting us.</label>');

                }

            }
        });//end of ajax

     }//end of submit handler
   });//end of validate
});//end of document.bind

【问题讨论】:

    标签: javascript jquery jquery-mobile


    【解决方案1】:

    您没有尝试正确的方法。要了解这种情况,您需要了解 jQuery Mobile 的工作原理。它使用 ajax 加载其他页面。

    首页正常加载。它的 HEAD 和 BODY 被加载到 DOM 中,它们在那里等待其他内容。当第二个页面被加载时,只有它的 BODY 内容被加载到 DOM 中。当我说只有 BODY 内容时,我的意思是只有一个页面 DIV(属性 data-role="page"),没有别的。这个问题有多种解决方案,这里是 list

    【讨论】:

    • 是的,这就是我做错的地方。现在我在页面启动后包含了我的 js 文件,现在它工作正常。非常感谢你的回答。我花了 3 个小时处理这个问题现在解决了。 :)
    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多