简体   繁体   English

包含PHP的JavaScript在Jquery Mobile中无法正常工作

[英]PHP included JavaScript is not working properly in Jquery Mobile

In my jquery mobile web app I include a Login-Form on every page the user is navigating to. 在我的jquery移动Web应用程序中,我在用户导航到的每个页面上都包含一个Login-Form。 I do that so that the user could login at every time he wants to, not just on the start page. 我这样做是为了使用户可以在每次他想登录时登录,而不仅仅是在开始页面上登录。

Since I do the Form submitting procedure with my very own Ajax logic, I disabled the Jquery Mobile Ajax logic with data-ajax="false" on the Form. 由于我使用自己的Ajax逻辑执行表单提交过程,因此我在表单上使用data-ajax="false"禁用了Jquery Mobile Ajax逻辑。 The Ajax logic is implemented with JavsScript. Ajax逻辑是用JavsScript实现的。 On the start page everything works fine, but if I navigate to another page (through a link on the start page), my JavaScript is not firing anymore, but the form is submitted via the Jquery mobile own Ajax logic (and therefore it don't works). 在开始页面上,一切正常,但是,如果我导航到另一个页面(通过开始页面上的链接),则我的JavaScript不再触发,但是该表单是通过Jquery移动设备自己的Ajax逻辑提交的(因此,它不会t作品)。

The code (which I include at every page) looks like this: 代码(我在每个页面中都包含)如下所示:

   <div data-role="fieldcontain">
        <form id="loginForm" data-ajax="false" onsubmit="login();return false;">
            <div data-role="fieldcontain">
                <h2>Login</h2>
                <label for="textinput1">
                    Email
                </label>
                <input name="emaillogin" id="textinput1" placeholder="Email" value=""
                type="text">
                <label for="textinput2">
                    Password
                </label>
                <input name="passwordlogin" id="textinput2" placeholder="Password" value=""
                type="password">
            </div>
            <input type="submit" data-icon="ok" data-iconpos="left" value="OK">
            <input type="hidden" name="inputCase" value="login">
        </form>
    </div>

The JavaScript (which is just at the end of the Code stated above) looks like that: JavaScript(位于上述代码的末尾)如下所示:

<script>
function login()
{
    var request = $.ajax({
      url: "../case.php",
      type: "POST",
      data: $('#loginForm').serialize(),
      dataType: "json"
    });

    request.done(function(msg) {
            if(parseInt(msg.status)==1)
            {
                //top_notification("Willkommen zurück!","success");
                window.location="index.php";
            }
            else if(parseInt(msg.status)==0)
            {
                alert(msg.text);
            }
            else {
                alert("Gibts nicht");
            }
    });

    request.fail(function(jqXHR, textStatus) {
        alert("Fehler");
    });
}
</script>

Maybe I got the Jquery Mobile "we replace just the page-div with the other page-div from the new URL" thing wrong, but I understand it in that way that my whole JS logic will also be pulled from the new ressource. 也许我弄错了Jquery Mobile“我们只是用新URL中的另一个page-div替换了page-div ”,但是我理解这种方式,我的整个JS逻辑也将从新资源中提取。

EDIT Thanks. 编辑谢谢。 I have updated my JS code, which looks now like that: 我已经更新了我的JS代码,现在看起来像这样:

<script>
$(document).on('pageinit', '[data-role="page"]', function(){ 
    $(document).on('click','#submit-btn',function() {
        login();
    });      
});

function login()
{
    var request = $.ajax({
      url: "../case.php",
      type: "POST",
      data: $('#loginForm').serialize(),
      dataType: "json"
    });

    request.done(function(msg) {
            if(parseInt(msg.status)==1)
            {
                //top_notification("Willkommen zurück!","success");
                window.location="index.php";
            }
            else if(parseInt(msg.status)==0)
            {
                alert(msg.text);
            }
            else {
                alert("Gibts nicht");
            }
    });

    request.fail(function(jqXHR, textStatus) {
        alert("Fehler");
    });
}
</script>

BUT. 但。 Now when I navigate to 3 pages, and then submit the login Form, I will get 3 alerts (even when I navigate to just 1 site) of the request.fail function... after that the login goes correctly! 现在,当我导航到3页,然后提交登录表单时,我将收到3条关于request.fail函数的警报(即使仅导航到1个站点)……之后,登录正确!

Ajax is still your problem. Ajax仍然是您的问题。 You have disabled ajax form submition but ajax is still used to load additional pages. 您已经禁用了ajax表单提交功能,但仍然使用ajax加载其他页面。 This is just my assumption because you didn't mentioned that ajax is turned off all together. 这只是我的假设,因为您没有提到ajax一起关闭。

If ajax is still used to load pages all your other pages are loaded into the DOM. 如果仍然使用ajax加载页面,则所有其他页面都将加载到DOM中。 Because of this you will have multiple forms with a same ID. 因此,您将拥有具有相同ID的多个表单。 When your first page is loaded there's only 1 form in a DOM and that form is used. 加载第一页时,DOM中只有一种形式,并且使用该形式。 But when another pages is loaded then additional form (with a same id) is added to the DOM. 但是,当加载其他页面时,会将其他表单(具有相同的ID)添加到DOM。 And whey you click a submit button jQuery will find first form with that ID from the DOM. 然后,您单击提交按钮,jQuery将从DOM中找到具有该ID的第一个表单。 And because there are 2 of them it will submit first for, same form loaded with an initial page. 并且由于其中有2个,它将首先提交,并以相同的表格加载初始页面。

That is why you NEVER use inline javascript with jQuery Mobile. 这就是为什么您永远不要在jQuery Mobile中使用内联javascript。

Instead of 代替

onclick="..."

Your submit button should have an id and make it type="button" . 您的提交按钮应具有一个id ,并将其设置为type="button"

<input type="button" data-icon="ok" data-iconpos="left" value="OK" id="submit-btn">

Put a click event on every button and use a $.mobile.activePage selector to find a form on an currently active page. 在每个按钮上放置一个click事件,并使用$ .mobile.activePage选择器在当前活动页面上查找表单。

$(document).on('click','#submit-btn',function() {
    $.mobile.activePage.find('#loginForm').submit();
});

Also everything should be wrapped inside a correct jQuery Mobile page event : 同样,所有内容都应包含在正确的jQuery Mobile 页面事件中

$(document).on('pageinit', '[data-role="page"]', function(){ 
    $(document).on('click','#submit-btn',function() {
        $.mobile.activePage.find('#loginForm').submit();
    });      
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM