여러 체크박스 중 하나의 선택을 필수로 설정
                                            
                                                
                                                
                                                
                                                
                                                
                                                 3380 단어  ServiceNowServiceNow 개발 메모
                    
개요
두 개 이상의 항목을 선택해야 하는 스크립트입니다. ServiceNow 포럼에서 질문에 대한 답변입니다.
하나 이상의 선택을 필요로 하는 경우 UI 정책에서 하나의 확인란을 필수로 설정하면 확인을 필수로 설정할 수 있습니다.
 
둘 이상의 선택을 필수로 하려면 UI 정책에서 두 개의 확인란을 필수로 설정하더라도 하나의 선택으로 등록할 수 있습니다.
두 개 이상의 선택을 필수로 하려면 클라이언트 스크립트를 작성해야 합니다.
 절차
 변수 정의
다음과 같이 레이블과 세 개의 확인란을 정의합니다.
 
 UI 정책
최소한 하나의 선택을 필요로 하기 위해 UI 정책을 만듭니다.
 
양식을 열어 보면 다음과 같이 라벨 오른쪽에 빨간색 '*'가 표시되어 선택이 필수임을 나타냅니다.
 
그러나 불행히도 하나를 선택하면 검게 변합니다. 이것은 어쩔 수 없다고 가정합니다.
 
 클라이언트 스크립트
다음 스크립트를 작성합니다. 변수 "mandatoryCount"는 선택 필수 항목의 수입니다. 이번에는 2개의 선택을 필수로 하므로 「2」로 합니다.
function onSubmit() {
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'club_wembely_membership,education,player_status_competitions';
    var mandatoryCount = 1;
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if (!passed) {
        //Abort the submit
        var message = 'You must select at least ' + mandatoryCount + ' options.';
        g_form.addErrorMessage(message);
        return false;
    }
}
function forceMandatoryCheckboxes(mandatory, count) {
    //Split the mandatory variable names into an array
    mandatory = mandatory.split(',');
    var answer = false;
    var varFound = false;
    var numTrue = 0;
    //Check each variable in the array
    for (var x = 0; x < mandatory.length; x++) {
        //Check to see if variable exists
        if (g_form.hasField(mandatory[x])) {
            varFound = true;
            //Check to see if variable is set to 'true'
            if (g_form.getValue(mandatory[x]) == 'true') {
                numTrue ++;
                //Exit the loop if we have reached required number of 'true'
                if (numTrue >= count) {
                    answer = true;
                    break;
                }
            }
        }
    }
    //If we didn't find any of the variables allow the submit
    if (varFound == false) {
        answer = true;
    }
    //Return true or false
    return answer;
}
 실행
양식을 열고 시도해보십시오. 항목 1개를 선택한 것만으로 "Order Now"버튼을 누르면 다음과 같이 에러 메시지가 표시됩니다.
 
이상
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(여러 체크박스 중 하나의 선택을 필수로 설정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/htshozawa/items/77b11cc197c047f13d21
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
변수 정의
다음과 같이 레이블과 세 개의 확인란을 정의합니다.

UI 정책
최소한 하나의 선택을 필요로 하기 위해 UI 정책을 만듭니다.

양식을 열어 보면 다음과 같이 라벨 오른쪽에 빨간색 '*'가 표시되어 선택이 필수임을 나타냅니다.

그러나 불행히도 하나를 선택하면 검게 변합니다. 이것은 어쩔 수 없다고 가정합니다.

클라이언트 스크립트
다음 스크립트를 작성합니다. 변수 "mandatoryCount"는 선택 필수 항목의 수입니다. 이번에는 2개의 선택을 필수로 하므로 「2」로 합니다.
function onSubmit() {
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'club_wembely_membership,education,player_status_competitions';
    var mandatoryCount = 1;
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if (!passed) {
        //Abort the submit
        var message = 'You must select at least ' + mandatoryCount + ' options.';
        g_form.addErrorMessage(message);
        return false;
    }
}
function forceMandatoryCheckboxes(mandatory, count) {
    //Split the mandatory variable names into an array
    mandatory = mandatory.split(',');
    var answer = false;
    var varFound = false;
    var numTrue = 0;
    //Check each variable in the array
    for (var x = 0; x < mandatory.length; x++) {
        //Check to see if variable exists
        if (g_form.hasField(mandatory[x])) {
            varFound = true;
            //Check to see if variable is set to 'true'
            if (g_form.getValue(mandatory[x]) == 'true') {
                numTrue ++;
                //Exit the loop if we have reached required number of 'true'
                if (numTrue >= count) {
                    answer = true;
                    break;
                }
            }
        }
    }
    //If we didn't find any of the variables allow the submit
    if (varFound == false) {
        answer = true;
    }
    //Return true or false
    return answer;
}
실행
양식을 열고 시도해보십시오. 항목 1개를 선택한 것만으로 "Order Now"버튼을 누르면 다음과 같이 에러 메시지가 표시됩니다.

이상
Reference
이 문제에 관하여(여러 체크박스 중 하나의 선택을 필수로 설정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/htshozawa/items/77b11cc197c047f13d21텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)