Link to home
Start Free TrialLog in
Avatar of jwebster77
jwebster77

asked on

Button to fire javascript only if @Html.EditorFor is equal to "Closed" in MVC

Hello,
I have a save button on the edit page that calls a javascript function when clicked.

<input type="submit" value="Save" class="btn btn-default" name="action" onclick="Confirm()" />

Open in new window


How can I make this button call the javascript function only if a dropdown contains the selection "Closed"?

This is the HTML code for the dropdown:
@Html.LabelFor(model => model.vcStatus, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(
                                        m => m.vcStatus,      
                                        Model.StatusList)     
                                        
                    @Html.ValidationMessageFor(model => model.vcStatus, "", new { @class = "text-danger" })
                </div>

Open in new window


Thank you very much.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

What does the rendered html/js look like?
Avatar of jwebster77
jwebster77

ASKER

Hi, do you mean to see the Javascript code?
function Confirm() {
    var input = $("<input />");
    input.attr("type", "hidden").attr("name", "confirm_value");
    if (confirm("Do you want to save the ticket to the knowledge base archive?")) {
        input.val("Yes");
    } else {
        input.val("No");
    }
    $("form")[0].appendChild(input[0]);
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.