简体   繁体   English

传递到Javascript函数的链接

[英]Passing a link to a Javascript function

My code works, but I'd like to clean it up and possibly reuse it instead of copying it. 我的代码可以工作,但是我想清理它并可能重用它而不是复制它。

I have a select box with each value selected checking or unchecking a group of checkboxes based on their class tag. 我有一个选择框,每个选择的值都根据其类标记选中或取消选中一组复选框。

Is it possible to pass the class name .task_mod to a function where the following 是否可以将类名称.task_mod传递给以下函数

$('.inside_border a.task_mod :checkbox').attr('checked', true);
$('.inside_border a.task_mod').css("background-color","#9dc6f4");
$('.inside_border a.task_mod').css("color","#fff");

can all be applied? 可以全部应用吗? That way, I can use this function for all the other checkboxes like so 这样,我可以对所有其他复选框使用此功能,如下所示

check_mod(".links_mod"); check_mod(“。links_mod”);

$('[name="workspace_type"]').change(function()
    {
        if($(this).val() == "0")
        {
            $('.inside_border a :checkbox').attr('checked', false);
            $('.inside_border a').css("background-color","#dfdfdf");
            $('.inside_border a').css("color","#6e6d6d");
        }
        else if($(this).val() == "small_biz")
        {
            $('.inside_border a.task_mod :checkbox').attr('checked', true);
            $('.inside_border a.task_mod').css("background-color","#9dc6f4");
            $('.inside_border a.task_mod').css("color","#fff");
        }
    });

The Selectbox 选择框

<select name="workspace_type">
                                    <option value="0">select please</option>
                                    <option value="small_biz">Small Business</option>
                                    <option value="freelance">Freelancer</option>
                                    <option value="startup">Startup Company</option>
                                    <option value="small_team">Small Team</option>
                                    <option value="academic">Academic</option>
                                    <option value="media">Media Organization</option>
                                    <option value="personal">Personal</option>
                                </select>

The checkboxes 复选框

<div class="inside_border">
<a href="#" class="task_mod"><input type="checkbox" name="add_mod"/>Tasks</a>
<a href="#" class="docs_mod"><input type="checkbox" />Documents</a>
<a href="#" class="discuss_mod"><input type="checkbox" />Discussions</a>
<a href="#" class="links_mod"><input type="checkbox" />Links</a>
<a href="#" class="images_mod"><input type="checkbox" />Images</a>
<a href="#" class="customer_mod"><input type="checkbox" />Customers</a>
<a href="#" class="project_mod"><input type="checkbox" />Projects</a>
<a href="#" class="members_mod"><input type="checkbox" />Members</a>
</div>

If you don't understand, I can explain. 如果您听不懂,我可以解释。 PS I know I should use Id(s) for the checkboxes but... PS我知道我应该为复选框使用ID,但是...

create this function: 创建此功能:

function check_mod($target){
    $target.css({
        "background-color": "#9dc6f4",
        "color":"#fff"
    })
    .find(":checkbox").attr('checked', true);
}

and than use like this: 而不是像这样使用:

check_mod($('.inside_border a.task_mod'));

当然

$('.inside_border a.'+class+' :checkbox')

可以通过$(this).parent()[0].className

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

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