繁体   English   中英

我如何使2个函数与jquery中的“或”同时工作?

[英]how do i make 2 functions work at same time with “or” in jquery?

我在检查单选按钮时正在执行某些操作,但是,现在我希望在页面加载时进行相同的操作,希望不会重复大量代码。

我为页面加载添加它的原因是因为我正在创建一个编辑/更新页面。 第一页用于将表单数据保存到mysql数据库,第二页从数据库中获取值并显示保存时的表单,一个人可以进行更改并将其保存回数据库。

所以我有这个点击。 对于其他单选值,此功能远不止于此,但不想粘贴太多。 这只是示例:

$("input[name='typeofmailerradio']").click(function() {
    if(this.value == 'Postcards') {
        $('#typeofpostcardmaileroptions').show("fast");
    }
    else {
        $('#typeofpostcardmaileroptions').hide("fast");
    }
    //more if/else's for more radio values here
});

所以我做了一个测试,并将所有这些都放入页面代码中:

$("input[name='typeofmailerradio']:checked").val(function() {
if(this.value == 'Postcards') {
        $('#typeofpostcardmaileroptions').show("fast");
    }
    else {
        $('#typeofpostcardmaileroptions').hide("fast");
    }
    //more if/else's for more radio values here
});

首先,这就是我复制大量代码的意思,其次,它并没有真正起作用。 它适用于页面加载,但是单击的代码部分不想再使用了。

我想做的是“或”之类的东西。 像这样的东西,但是不知道如何去做:

$("input[name='typeofmailerradio']").click(function() || $("input[name='typeofmailerradio']:checked").val(function() {
    if(this.value == 'Postcards') {
        $('#typeofpostcardmaileroptions').show("fast");
    }
    else {
        $('#typeofpostcardmaileroptions').hide("fast");
    }
    //more if/else's for more radio values here
});

顺便说一句,我写的建议解决方案根本不起作用。 在加载时不起作用,并且在单击时不起作用。

不起作用:

$("input[name='typeofmailerradio'],input[name='typeofmailerradio']:checked").click(function() {
if(this.value == 'Postcards') {
    $('#typeofpostcardmaileroptions').show("fast");
}
else {
    $('#typeofpostcardmaileroptions').hide("fast");
}
//more if/else's for more radio values here
});

不起作用:

$("input[name='typeofmailerradio'],input[name='typeofmailerradio']:checked").val(function() {
if(this.value == 'Postcards') {
    $('#typeofpostcardmaileroptions').show("fast");
}
else {
    $('#typeofpostcardmaileroptions').hide("fast");
}
//more if/else's for more radio values here
});

这就是我所做的,以使其发挥作用...

$(document).ready(function() {
  function typeOfMailer() {

if($('#typeofmailerradio1').is(':checked')) {   
    $('#typeofpostcardmaileroptions').show("fast");
    $('#snapapartoption').hide("fast");
    $('#typeofspecialtymaileroptions').hide("fast");
    $('#typeofmaileroptions').hide("fast");
}

else if($('#typeofmailerradio2').is(':checked')) {
    $('#typeofpostcardmaileroptions').hide("fast");
    $('#snapapartoption').show("fast");
    $('#typeofspecialtymaileroptions').hide("fast");
    $('#typeofmaileroptions').hide("fast");
}

else if($('#typeofmailerradio3').is(':checked')) {
    $('#typeofpostcardmaileroptions').hide("fast");
    $('#snapapartoption').hide("fast");
    $('#typeofspecialtymaileroptions').show("fast");
    $('#typeofmaileroptions').hide("fast");
}

else {
    $('#typeofpostcardmaileroptions').hide("fast");
    $('#snapapartoption').hide("fast");
    $('#typeofspecialtymaileroptions').hide("fast");
    $('#typeofmaileroptions').show("fast");
}

}

$("input[name='typeofmailerradio']").click(function() {
typeOfMailer();
});

typeOfMailer();
});

您可以使用多个选择器:

$('input[name=foo],input[name=bar]:checked').whatever();
                  ^---separate them with a comma

尝试类似...

$(document).ready( function() {

    function do_stuff() {
        if(this.value == 'Postcards') {
            $('#typeofpostcardmaileroptions').show("fast");
        }
        else {
            $('#typeofpostcardmaileroptions').hide("fast");
        }
        //more if/else's for more radio values here
    }

    $("input[name='typeofmailerradio']").click(function() {
        do_stuff();
    });

    do_stuff();

}

您不能真正“重用”匿名函数。 因此定义它们。

暂无
暂无

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

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