0

I have a ASP.NET MVC view with a checkbox in a form. Is there a way to change the checkbox value when the submit button is clicked?

I have a form with two text inputs and a checkbox. I have two submit buttons, when submit 1 is clicked I just want to submit the form as it is. But, when submit 2 is click I want to automatically set the checkbox checked and then post the form to the controller.

Is there a way to do this? In the view or with javascript?

2 Answers 2

1

there is two way to solve this. first you can set event on click on the second button

<input name="submit" type="submit"  id="JustSubmit"/>
<input name="submit" type="submit" onclick="checkedCheckbox()" id="SubmitWithCheckbox"/>

on the javascript

function checkedCheckbox()
{
   $('#myCheckbox').prop('checked', true);
}

Second one by using server side. you can name both of your submit button to validate and change the value the checkbox based on which the submit button is pressed.

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />

and in the controller you can validate which submit button you check

public ActionResult Index(Class Model, string submit)
{
   if(submit == "process")
   {
        Model.Checked = true;
    }
//if not...
    return View();
}
Sign up to request clarification or add additional context in comments.

Comments

0

It is hard to understand what are you trying to achieve without looking at your code or example in JSFiddle.

but if i understand you right you can try something like this

$(document).ready(function () {

  $("#submit2").on("click", function() {
     $("#checkbox").attr("checked", true);          

       // HERE YOU CAN DO SOME OTHER CODE TO SUBMIT FORM
  });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.