即使没有待保存的挂起更改,Windows beforeunload也会发出警报消息

时间:2016-04-21 02:37:43

标签: jquery asp.net

我使用以下jQuery在用户离开指定页面而不点击“保存/更新”按钮时提醒用户。它按预期工作,但即使没有任何挂起的更改要保存,它也会发出警报消息。当用户保存所做的所有更改并尝试导航离开页面时,它会提醒用户。我希望它只有在没有保存更改时才会发出警报。

   using UnityEngine;
using System.Collections;

public class Bomb : MonoBehaviour
{


    public AudioClip clipBomba;

    private Heart heart;
    private Gerenciador gerenciador;

    void Awake ()
    {


    }

    // Use this for initialization
    void Start ()
    {
        gerenciador = FindObjectOfType (typeof(Gerenciador)) as Gerenciador;
    }

    // Update is called once per frame
    void Update ()
    {



    }

    void OnCollisionEnter2D (Collision2D colisor)
    {
        if (colisor.gameObject.tag == "floor") {

            Destroy (gameObject, 2f);

        } else {

            if (colisor.gameObject.tag == "Bee") {

                Som();

                heart= GameObject.FindGameObjectWithTag ("Heart").GetComponent<Vidas> () as Vidas;

                if (heart.TakeHeart ()) {
                    Destroy (gameObject);

                } else {

                    gerenciador.GameOver ("GameOver");

                }
            }
        }
    }


    void Som()
    {

        GetComponent<AudioSource> ().clip = clipBomb;
        AudioSource.PlayClipAtPoint (clipBomb, transform.position);
    }
}

2 个答案:

答案 0 :(得分:0)

因为你没有其他条件...... 如果要在unsave为false时禁用警报,

$(window).bind('beforeunload', function () {
            return isSave();
        });

function isSave() {
    if (unsaved) {
        return "You have made some changes. Save your pending changes before existing the page";
    }
    else {
        window.beforeunload = null;
        window.unbind();
    }
}

答案 1 :(得分:0)

在发生保存后,我不知道你将未保存的地址设置为false。尝试以下代码,它可能有效

        $(document).ready(function () { 
            $(document).on('change', ':input', function (e) {
                $(this).addClass('changed-input');
            });

            $(window).on('beforeunload', function () {
                if ($('.changed-input').length) {
                    return 'You haven\'t saved the changes you made.';
                }
            });
        })

参考: link