jowan
jowan

Reputation: 67

How to auto submit when all radio selected?

I want to auto submit when all radio selected, here is my code.

<form name="quiz" ng-submit="quiz.answer(selected)">
  <label>
    <input type="radio" ng-model="selected" ng-value="red">
    Red
  </label><br/>
  <label>
    <input type="radio" ng-model="selected" ng-value="green">
    Green
  </label><br/>
  <input type="submit" id="submit" value="Submit" />
</form>

Upvotes: 1

Views: 491

Answers (3)

Naser.Sadeghi
Naser.Sadeghi

Reputation: 1302

You should add a java-script function and trigger it every time the user clicks on one of the radio buttons.

Here, as there are only two radio buttons, I have used && but in a case that the number of them is more than two you can have a counter value for all of them as well.

Here's the code that you can use:

function check() {
  if (document.getElementById("first").checked && document.getElementById("second").checked) {
    alert("triggered");
    document.getElementById('myForm').submit();
  }
}
<!DOCTYPE html>
<html>

<body>


  <form name="quiz" id="myForm" action="https://www.google.com">
    <label>
    <input type="radio" ng-model="selected" ng-value="red" id="first" onClick="check();">
    Red
  </label><br/>
    <label>
    <input type="radio" ng-model="selected" ng-value="green" id="second" onClick="check();">
    Green
  </label><br/>
    <input type="submit" id="submit" value="Submit" />
  </form>
</body>

</html>

Upvotes: 1

Nikhil Singh
Nikhil Singh

Reputation: 1610

You can give check whether the radio is selected or not using JS. Later you can click the submit button using JS if the condition meets your use case.

To click button:

document.getElementById('submit').click();

Upvotes: 0

Zak
Zak

Reputation: 7515

Add an onClick() event ...

Give your form an ID

 <form id="quiz" name="quiz" ng-submit="quiz.answer(selected)">

Then call that ID and submit

<input type="radio" ng-model="selected" ng-value="green"
     onClick="document.getElementById('quiz').submit();">

Upvotes: 0

Related Questions