Home > DeveloperSection > Forums > Check condion before post page on button click in Jquery or JavaScript.
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    February-02-2013 2:34 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 1100  View(s)
Rate this:

Hi Expert!

I want to check condition before post my page on button click in MVC 4 using JavaScript or JQuery. My Code as below

@using (Html.BeginForm("AttachmentList", "Home", FormMethod.Post, new
{
    @id = "PDFListDownload"
}))
{
    <div id="PDFAttachmentList">
        <table>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@Html.CheckBox("fileItem")
                        @Html.Hidden("fileId", item.AtttachmentID)
                    </td>
                    <td>@Html.DisplayTextFor(m => item.Description)
                    </td>
                </tr>
            }
            <tr>
                <td>
                    <input id="btnDownload" type="submit" value="Download"  />
                </td>
                <td>
                    <input id="btnCancel" type="button" value="Cancel" onclick="CloseDialog();" />
                </td>
            </tr>
        </table>
    </div>
}

How it I do? Please help me?

Thanks in advance



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    February-02-2013 5:30 AM

Hi Samuel Fernandes!

I think, you want to force user to select at least one checkbox checked before post page according your code, if I'm right than you can try below line of code

<script type="text/javascript">
 $(document).ready(function () {
$('#btnDownload').live("click", function () {
                var checked = $("#PDFAttachmentList input:checked").length > 0;
                if (!checked) {
                    alert("Please check at least one checkbox");
                    return false;
                }
                else {
                    return true;
                }
            });
});


Don't want to miss updates? Please click the below button!

Follow MindStick