简体   繁体   中英

How to call a function depending on the radio button selected

I have two functions and i want to call one function when the radio button is checked as an employee and the other when the radio button is checked as a user.

employee.form.send = function() {
   employee.validator.checkForm();
   if (employee.validator.valid()) {

    employee.form.submit();
   }
  };


 invite.form.send = function() {
   invite.validator.checkForm();
   if (invite.validator.valid()) {
    alert(1);
     invite.form.submit();
   }
 }

I would normally call them with a click event like this

invite.find('#sendInviteButton').on('click',invite.form.send);

Now i want to call different functions when the #sendInviteButton is clicked. Depending on the radio button selected. How do i do that? I am not able to call the invite.form.send inside an if condition.

what about binding the event to a function which checks the state of the ratio button and than depending on that call the proper functions accordingly? (Check code below)


invite.find('#sendInviteButton').on('click', checkState); // update the event binding

function checkState() {
    if (document.getElementById('sendInviteButton').checked) {
        // call function if #sendInviteButton is checked
    } else {
        // call other function
    }
}

or in case you use jQuery:

function checkState() {
    if($('#sendInviteButton').is(':checked')) {
        // call function if #sendInviteButton is checked
    } else {
       // call other function
    }
}

If I understood correctly, you want to do something like this:

    if($('#employee_radio_button').is(':checked'))
         employee.form.send();
    else
         invite.form.send();

You could wrap the click into a function with jQuery like so, assuming your function names are "oneFunction" and "anotherFunction"..

$('#sendInviteButton').click(function() { 
 if ($('#sendInviteButton').is(':checked')) {
   oneFunction();
  } else {
   anotherFunction();
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM