简体   繁体   English

是否可以将这两个功能合二为一?

[英]is it possible to combine these two functions into one?

I have two functions that are basically identical.我有两个基本相同的功能。 The first function gets passed a mouse event where event.target is the input type='checkbox' the second function gets passed the input type='checkbox' is there a way to rewrite these into one function?第一个函数传递了一个鼠标事件,其中 event.target 是输入类型='checkbox' 第二个函数传递了输入类型='checkbox' 有没有办法将它们重写为一个函数?

function crewChecked(event){                
    if(event.target.checked){
        names.push(event.target.value)
    }else{
        for(let i = 0; i < names.length;i++){
            if(names[i] == event.target.value){
                names = names.filter((name, index) => index !== i);
                break;
            }
        }
    }
    if(names.length==0){
        document.getElementById('dropdownCrewButton').innerText = "Select Crew"
    }else{
        document.getElementById('dropdownCrewButton').innerText = names.length + " crew"
    }
}


function crewChecked2(crewCheckbox){
    if(crewCheckbox.checked){
        names.push(crewCheckbox.value)
    }else{
        for(let i = 0; i < names.length;i++){
            if(names[i] == crewCheckbox.value){
                names = names.filter((name, index) => index !== i);
                break;
            }
        }
    }
    if(names.length==0){
        document.getElementById('dropdownCrewButton').innerText = "Select Crew"
    }else{
        document.getElementById('dropdownCrewButton').innerText = names.length + " crew"
    }
}

The only part of these two functions that differ are how you get the reference to the checkbox.这两个函数中唯一不同的部分是如何获取对复选框的引用。 So if you figure out how to get that checkbox in both cases, you're golden.因此,如果您弄清楚如何在这两种情况下获得该复选框,那么您就是黄金。

Something like:就像是:

function crewChecked(eventOrCheckbox){          
      
    // Added
    const checkbox = eventOrCheckbox.target || eventOrCheckbox

    if(checkbox.checked){
        names.push(checkbox.value)
    }else{
        for(let i = 0; i < names.length;i++){
            if(names[i] == checkbox.value){
                names = names.filter((name, index) => index !== i);
                break;
            }
        }
    }
    if(names.length==0){
        document.getElementById('dropdownCrewButton').innerText = "Select Crew"
    }else{
        document.getElementById('dropdownCrewButton').innerText = names.length + " crew"
    }
}

This line checks for a target property on the eventOrCheckbox argument.此行检查eventOrCheckbox参数上的target属性。 If there is one, then it's an event and the checkbox should be saved to checkbox .如果有,那么它是一个事件,并且复选框应该保存到checkbox If there isn't, then assume this argument is the checkbox and continue on.如果没有,则假设此参数复选框并继续。

The only difference is whether you test event.target.checked and event.target.value or crewCheckbox.checked and crewCheckbox.value .唯一的区别是你是测试event.target.checkedevent.target.value还是crewCheckbox.checkedcrewCheckbox.value crewChecked can call crewChecked2 , passing event.target as the argument. crewChecked可以调用crewChecked2 ,传递event.target作为参数。

function crewChecked(event) {
    crewChecked2(event.target);
}

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

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