簡體   English   中英

如何根據所選的單選按鈕調用功能

[英]How to call a function depending on the radio button selected

我有兩個功能,當我選擇單選按鈕作為雇員時,我想調用一個功能,而當另一個單選按鈕作為用戶檢查時,我想調用另一個功能。

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();
   }
 }

我通常會用這樣的click事件來稱呼他們

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

現在,我想在單擊#sendInviteButton時調用不同的函數。 取決於選擇的單選按鈕。 我怎么做? 我無法invite.form.send inside if條件下調用invite.form.send inside

將事件綁定到檢查比率按鈕狀態的函數,然后根據該事件相應調用適當的函數呢? (下面的檢查代碼)


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
    }
}

或者如果您使用jQuery:

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

如果我理解正確,則您想執行以下操作:

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

您可以像這樣用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