jquery如何结合两个条件来启动函数

时间:2014-04-27 12:50:32

标签: jquery

如果用户点击id^="mark_paid_used"或在课程paid_used_amount_for_below_header_debit中更改了值

,则需要启动功能

此类代码有效

 $('[id^="mark_paid_used"]').click(function(){
     //do something
 });

 $('.paid_used_amount_for_below_header_debit').change(function() {
     //do the same as above
 });

但这不起作用

$('.paid_used_amount_for_below_header_debit').change || $('[id^="mark_paid_used"]').click(function() {

 });

什么是正确的代码?

3 个答案:

答案 0 :(得分:2)

您可以创建一个函数,就像我在示例中所做的那样。之后,您可以将其传递给事件处理程序

var yourFunction = function () {
    //do something
};

$('[id^="mark_paid_used"]').click(yourFunction);
$('.paid_used_amount_for_below_header_debit').change(yourFunction);

答案 1 :(得分:1)

调用函数,然后将其添加为事件

var getfunction = function(){

 };


$('[id^="mark_paid_used"]').click(getfunction);
     $('.paid_used_amount_for_below_header_debit').change(getfunction);

答案 2 :(得分:0)

定义一个函数,并在发生某些事情时执行它。

function anyname(){
    // your code
}

$('.paid_used_amount_for_below_header_debit').change(function(){anyname();});
$('[id^="mark_paid_used"]').click(function(){anyname()});