2017-09-23 26 views
1

我有兩個函數,當單選按鈕選中爲員工時,我想調用一個函數,另一個選中單選按鈕作爲用戶。如何調用一個函數,取決於所選的單選按鈕

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

我通常會用一個點擊事件打電話給他們這樣

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

現在,我要的是點擊#sendInviteButton時要調用不同的功能。取決於所選的單選按鈕。我怎麼做? 如果有條件,我無法撥打invite.form.send inside

回答

0

將事件綁定到檢查比例按鈕狀態的函數,而不是根據相應的函數調用正確的函數呢? (下面校驗碼)


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

我有我的功能這樣的'employee.form.send =功能( ){ employee.validator.checkForm(); if(employee.validator.valid()){ employee.form.submit(); } }'並且我無法稱之爲'employee.form.send' – Sourav

+0

您必須將其稱爲:'employee.form.send()'... 或對不起,如果我誤解了,但我不要得到你想告訴/問我的那個 – Kristianmitk

0

如果我理解正確的,你想要做這樣的事情:

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

你可以換點擊進入功能使用jQuery像這樣,假設你的函數名是「oneFunction」和「anotherFunction」 ..

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