Vian Irfandy
Vian Irfandy

Reputation: 39

Check value in Radio Button

FORM :

<form id="approvalPenambahanUserInt" name="approvalPenambahanUserInt" action="" method="post">

  <div class="form-group">
    <div class="col-sm-12">
      <label for="persetujuan" class="col-sm-2 control-label">Status</label>
      <div class="col-sm-5">
        <input type="radio" name="persetujuan" id="Approved" class="logintStatuses" value="1">&nbsp;&nbsp;&nbsp;&nbsp;Disetujui
        <input type="radio" name="persetujuan" id="Rejected" class="logintStatuses" value="2">&nbsp;&nbsp;&nbsp;&nbsp;Ditolak
      </div>
</form>

and I try this is not working.

JS :

$("#approvalPenambahanUserInt").submit(function(e) {
  let form = $(this);
    if ($('input[name="persetujuan"]:checked').length === 0){
    alert('Please Choose Status!');
    return false;
  }else if($('input[name="persetujuan"]:checked').length === 1){
    alert('Accepted!');
  }else {
    alert('Rejected!');}
});

can someone help i try to find if value 1 = Accepted and 2 is rejected

Upvotes: 0

Views: 52

Answers (2)

Amit Shakya
Amit Shakya

Reputation: 994

you shold check this radio button jquery

Main issue is let form = $(this); this line, I am sure when you using $(this) you are assuming it is form object but when you use this in javascript this should return window object.

Upvotes: 0

Mister Jojo
Mister Jojo

Reputation: 22422

document.forms.approvalPenambahanUserInt
  .addEventListener('submit', function(event)
  {
  event.preventDefault() // disable submit
  console.clear()
  if (!this.persetujuan.value)
    {
    console.log('Please Choose Status!')
    }
  else
    {
    console.log( this.persetujuan.value )
    }
  })
<form  name="approvalPenambahanUserInt" action="" method="post">

  <div class="form-group">
    <div class="col-sm-12">
      <label class="col-sm-2 control-label">Status</label>
      <div class="col-sm-5">
        <label>
          <input type="radio" name="persetujuan" value="Approved" class="logintStatuses" >
          Disetujui
        </label>
        <label>
          <input type="radio" name="persetujuan" value="Rejected" class="logintStatuses" >
          Ditolak
        </label>
      </div>
    </div>
  </div>
  <button type="submit">submit</button>
</form>

Upvotes: 2

Related Questions