简体   繁体   English

如何根据所选的单选按钮调用功能

[英]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 我通常会用这样的click事件来称呼他们

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

Now i want to call different functions when the #sendInviteButton is clicked. 现在,我想在单击#sendInviteButton时调用不同的函数。 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. 我无法invite.form.send inside if条件下调用invite.form.send inside

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: 或者如果您使用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".. 您可以像这样用jQuery将点击包装到一个函数中,假设您的函数名称是“ oneFunction”和“ anotherFunction”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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