Walter v. Kempen
Walter v. Kempen

Reputation: 1

jQuery form, check if radio is checked

Im having trouble getting values from the checked radio:

<form id="addform" /*action="catch.php" method="POST"*/>
<div class="form-group input-container">
    <label class="col-sm-7"> *Href looks like "/vacacy/12": </label>
    <input class="col-sm-3" name="vacancy_full_URL" value="0" type="radio" required>
    <label class="col-sm-7"> *Href looks like "https://www.test.com/vacancy/12": </label>
    <input class="col-sm-3" name="vacancy_full_URL" value="1" type="radio" required>
</div>
</form>

This is the JavaScript code:

 $( "#check" ).click(function()
        {
            var $inputs = $('#addform :input');

            var values = {};
            $inputs.each(function()
            {
                if (this.name === "vacancy_full_URL")
                {
                    if (/* check if radiobutton is checked */)
                    {
                        values[this.name] = $(this).val();
                    }
                }
                else
                {
                    values[this.name] = $(this).val();
                }

            });
        });

I'm having trouble finding the right functions to check this with. My earlier attempt if (this.checked() === true) did not work. Can you please help me?

Upvotes: 0

Views: 100

Answers (3)

Sabbir Ahmed
Sabbir Ahmed

Reputation: 11

Please try this.

$(document).ready(function () {
    $("#check").click(function () {
        var values = {};
        var other_data = $('input[type=radio]', $('#addform'));;
        $.each(other_data, function (key, input) {
            if (input.checked) {
                values[input.name + "_checked"] = input.value;
            } else {
                values[input.name+ "_unchecked"] = input.value;
            }
        });
    });
})

Upvotes: 0

Jayshri Ghoniya
Jayshri Ghoniya

Reputation: 266

You can handle click event on button and check radion click name with :checked

$(document).ready(function(){
      $("#check").click(function(){
          var radioValue = $("input[name='vacancy_full_URL']:checked").val();
           if(radioValue){
             alert("Your are a - " + radioValue);
             }
           });
        });

Upvotes: 1

brk
brk

Reputation: 50291

Use is(":checked") to find if a radio button is checked. Also there are few mistakes in html form like a you have make the form self closing. Secondly there was no item with id check

$("#check").click(function(e) {
  e.preventDefault()
  var $inputs = $('#addform :input');

  var values = {};
  $inputs.each(function() {
    if (this.name === "vacancy_full_URL" && $(this).is(":checked")) {
      values[this.name] = $(this).val();
    } else {
      values[this.name] = $(this).val();
    }
  });
  console.log(values)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="addform" action="catch.php" method="POST">
  <div class="form-group input-container">
    <label class="col-sm-7"> *Href looks like "/vacacy/12": </label>
    <input class="col-sm-3" name="vacancy_full_URL" value="0" type="radio" required>
    <label class="col-sm-7"> *Href looks like "https://www.test.com/vacancy/12": </label>
    <input class="col-sm-3" name="vacancy_full_URL" value="1" type="radio" required>
  </div>
  <button type='submit' id='check'>Click</button>
</form>

Upvotes: 1

Related Questions