在页面卸载时没有调用Jquery函数

时间:2022-08-23 11:41:36
                <input type="submit" name="Post" value="Post" class="btm-bg" />






 <script type="text/javascript" src="../Scripts/jquery-1.9.0.min.js"></script>
 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">  </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"  type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.min.js")" type="text/javascript"></script>

$(function () {

    $('.option').hide();

    $('input[class="Tproperty"]').click(function () {

        $('.option').hide();

        if ($(this).val() == '2' ||  $(this).val() == '8') {
            $('#Display').show();
            $('#residential').hide();
            $('#amen').hide();
        }
        if ( $(this).val() == '5') {
            $('#Display').show();
            $('#residential').hide();
            $('#amen').hide();
            $('#xtrafac').hide();
            $('#parkings').hide();
            $('#rooms').hide();
            $('#bathrooms').hide();

        }
        else {
            $('#Display').show();
            $('#residential').show();
            $('#amen').show();
            $('#xtrafac').show();
            $('#parkings').show();
            $('#rooms').show();
            $('#bathrooms').show();

        }
    });
});

$('form').on('submit', function () {
    alert("hello");
    var sr = $("input[name = saleorrent]:checked").val();
    alert(sr);
    var mr = $("input[name = Monthlyrent]").val();
    alert(sr + mr);
    if (sr == "Rent" && mr == null  ) {
        alert("Enter monthly rent");
        e.preventDefault();
    }

});

$(function () {

    $('.showes').hide();

    $('input[class="saleorrent"]').click(function () {

        $('.showes').hide();

        if ($(this).val() == 'Sale') {
            $('#optionsale').show();
            $('#sale').show();
            $('#rent').hide();
        }
        else {
            $('#optionRent').show();
            $('#sale').hide();
            $('#rent').show();
        }
    });
});

function CheckNumeric(e) {

function CheckNumeric(e){

    if (window.event) // IE 
    {
        if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
            event.returnValue = false;
            return false;
        }
    }
    else { // Fire Fox
        if ((e.which < 48 || e.which > 57) & e.which != 8) {
            e.preventDefault();
            return false;
        }
    }

}

$(function () {
    $("#City").change(function () {
        if ($("#City").val() != 0) {
            var ddlCityId = $(this).val();
            var subItems = "";
            $.getJSON("@Url.Action("ddlCities", "Listings")", { id: ddlCityId },
                    function (data) {
                        $.each(data, function (index, item) {
                            subItems += "<option value='" + item.Locality_Id + "'>" + item.Locality + "</option>"
                        });
                        $("#Locality").html(subItems)
                    });
        }
        else {
            alert("false");
        }
    });
});

I am using MVC 4.0 . I want an alert message if the field is left empty. But the thing is as soon as I click the submit button it reaches out to the server side and not executing the jquery code before unloading the page.

我正在使用MVC 4.0。如果该字段留空,我想要一条警告消息。但事情是,只要我点击提交按钮,它就会到达服务器端,而不是在卸载页面之前执行jquery代码。

1 个解决方案

#1


0  

beforeunload fires after a form submission as the form submission can be cancelled. You need to stop the form submission instead. e.g. like:

在表单提交后,beforeunload会触发,因为表单提交可以取消。您需要停止表单提交。例如喜欢:

$('form').on('submit', function () {
    alert("hello");
    var sr = $("input[name = saleorrent]:checked").val();
    alert(sr);
    var mr = $("input[name = Monthlyrent]").val();
    alert(sr+ mr);
    if (sr == 'Rent' &&  mr == " ")
    {
        alert("Enter monthly rent");
       e.preventDefault();      // <<<<<< Stop the form submission
    }

});

Notes:

笔记:

Always use prop('checked') with checkboxes.

始终使用prop('checked')复选框。

var sr = $("input[name=saleorrent]").prop('checked');

#1


0  

beforeunload fires after a form submission as the form submission can be cancelled. You need to stop the form submission instead. e.g. like:

在表单提交后,beforeunload会触发,因为表单提交可以取消。您需要停止表单提交。例如喜欢:

$('form').on('submit', function () {
    alert("hello");
    var sr = $("input[name = saleorrent]:checked").val();
    alert(sr);
    var mr = $("input[name = Monthlyrent]").val();
    alert(sr+ mr);
    if (sr == 'Rent' &&  mr == " ")
    {
        alert("Enter monthly rent");
       e.preventDefault();      // <<<<<< Stop the form submission
    }

});

Notes:

笔记:

Always use prop('checked') with checkboxes.

始终使用prop('checked')复选框。

var sr = $("input[name=saleorrent]").prop('checked');