简体   繁体   English

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

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

i have something being done when a radio is checked, however, now i want to make the same thing happen on page load, hopefully without duplicating the bulk of the code. 我在检查单选按钮时正在执行某些操作,但是,现在我希望在页面加载时进行相同的操作,希望不会重复大量代码。

the reason i'm adding it for page load is because i'm creating an edit/update page. 我为页面加载添加它的原因是因为我正在创建一个编辑/更新页面。 the first page is for saving form data to mysql database, and second page grabs values from database and displays the form as it was saved and a person can make changes and save it back to the database. 第一页用于将表单数据保存到mysql数据库,第二页从数据库中获取值并显示保存时的表单,一个人可以进行更改并将其保存回数据库。

so i have this for the click. 所以我有这个点击。 it's more than this for the other radio values, but don't want to paste too much. 对于其他单选值,此功能远不止于此,但不想粘贴太多。 this is just example: 这只是示例:

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

so i did a test and put all of this into the page code: 所以我做了一个测试,并将所有这些都放入页面代码中:

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

first, that's what i meant by duplicating the bulk of code, and second it didn't really work. 首先,这就是我复制大量代码的意思,其次,它并没有真正起作用。 it worked for page load, but the section of code for clicking didn't want to work anymore. 它适用于页面加载,但是单击的代码部分不想再使用了。

what i want to do is an "or" or something. 我想做的是“或”之类的东西。 something like this, but don't know how to go about it: 像这样的东西,但是不知道如何去做:

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

by the way, what the proposed solution i wrote doesn't work at all. 顺便说一句,我写的建议解决方案根本不起作用。 doesn't work on load and doesn't work when clicking. 在加载时不起作用,并且在单击时不起作用。

doesn't work: 不起作用:

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

doesn't work: 不起作用:

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

this is what i did to make it work... 这就是我所做的,以使其发挥作用...

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

You can use multiple selectors: 您可以使用多个选择器:

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

Try something like... 尝试类似...

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

}

You can't really "reuse" anonymous functions. 您不能真正“重用”匿名函数。 So define them. 因此定义它们。

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

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