Вопрос по preventdefault, validation, asp.net-mvc-3, jquery – Остановить отправку формы, используя Jquery

54

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

<input id="saveButton" type="submit" value="Save" />
<input id="cancelButton" type="button" value="Cancel" />
<script src="../../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("form").submit(function (e) {
            $.ajax({
                url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
                data: { id: '@Model.ClientId' },
                success: function (data) {
                    showMsg(data, e);
                },
                cache: false
            });
        });
    });
    $("#cancelButton").click(function () {
        window.location = '@Url.Action("list", "default", new { clientId = Model.ClientId })';
    });
    $("[type=text]").focus(function () {
        $(this).select();
    });
    function showMsg(hasCurrentJob, sender) {
        if (hasCurrentJob == "True") {
            alert("The current clients has a job in progress. No changes can be saved until current job completes");
            if (sender != null) {
                sender.preventDefault();
            }
            return false;
        }
    }

</script>
Error: User Rate Limit Exceeded Antarr Byrd
Error: User Rate Limit Exceeded DG3

Ваш Ответ

5   ответов
3

Error: User Rate Limit Exceedede.preventDefault() was addedError: User Rate Limit Exceeded

 $(document).ready(function () {
    $("form").submit(function (e) {
       $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: '@Model.ClientId' },
            success: function (data) {
                showMsg(data, e);
            },
            cache: false
        });
        e.preventDefault();
    });
});

Error: User Rate Limit Exceeded

Here is an example of some added validation

 $(document).ready(function () {
    $("form").submit(function (e) {
      /* put your form field(s) you want to validate here, this checks if your input field of choice is blank */
    if(!$('#inputID').val()){ 
       e.preventDefault(); // This will prevent the form submission
     } else{
        // In the event all validations pass. THEN process AJAX request.
       $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: '@Model.ClientId' },
            success: function (data) {
                showMsg(data, e);
            },
            cache: false
       });
     }


    });
 });
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Antarr Byrd
15

Error: User Rate Limit Exceeded

if(e.preventDefault) 
   e.preventDefault(); 
else 
   e.returnValue = false;

Becoz e.preventDefault() is not supported in IE( some versions ). In IE it is e.returnValue = false

Error: User Rate Limit Exceedede.preventDefault()Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededjQuery onError: User Rate Limit Exceededevent.stopPropagation()Error: User Rate Limit Exceededevent.preventDefault()Error: User Rate Limit ExceededfalseError: User Rate Limit Exceededfunction(){ return false; }Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
103

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceedede.preventDefault();Error: User Rate Limit Exceeded

$("form").submit(function (e) {
      e.preventDefault(); // this will prevent from submitting the form.
      ...

Error: User Rate Limit Exceeded

I want it to be allowed HasJobInProgress == False

$(document).ready(function () {
    $("form").submit(function (e) {
        e.preventDefault(); //prevent default form submit
        $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: '@Model.ClientId' },
            success: function (data) {
                showMsg(data);
            },
            cache: false
        });
    });
});
$("#cancelButton").click(function () {
    window.location = '@Url.Action("list", "default", new { clientId = Model.ClientId })';
});
$("[type=text]").focus(function () {
    $(this).select();
});
function showMsg(hasCurrentJob) {
    if (hasCurrentJob == "True") {
        alert("The current clients has a job in progress. No changes can be saved until current job completes");
        return false;
    } else {
       $("form").unbind('submit').submit();
    }
}
Error: User Rate Limit Exceeded Antarr Byrd
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Antarr Byrd
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Antarr Byrd
2

Error: User Rate Limit Exceeded

$('form').submit(function (e) {
            if (radioButtonValue !== "0") {
                e.preventDefault();
            }
        });
3

Error: User Rate Limit Exceeded

    <script type="text/javascript">
    function CheckData() {
    //you may want to check something here and based on that wanna return true and false from the         function.
    if(MySt,uffIsokay)
     return true;//will cause form to postback to server.
    else
      return false;//will cause form Not to postback to server
    }
    </script>
    @using (Html.BeginForm("SaveEmployee", "Employees", FormMethod.Post, new { id = "EmployeeDetailsForm" }))
    {
    .........
    .........
    .........
    .........
    <input type="submit" value= "Save Employee" onclick="return CheckData();"/>
    }

Похожие вопросы