複数チェックボックスの中で1つの選択を必須にする
3419 ワード
概要
二つ以上の項目の選択を必須にするスクリプトです。ServiceNowフォーラムでの質問の回答です。
一つ以上の選択を必須にする場合はUIポリシーで1つのチェックボックスを必須にするとチェックを必須にできます。
二つ以上の選択を必須にする場合はUIポリシーで2つのチェックボックスを必須にしても1つの選択で登録することができます。
二つ以上の選択を必須にするにはクライアントスクリプトを作成する必要があります。
手順
変数の定義
UIポリシー
最低一つの選択を必須にするためにUIポリシーを作成します。
フォームを開いて見ると次のようにラベルの右に赤い「*」が表示されて選択が必須であることを示します。
但し残念なことに1つを選択すると黒くなります。これはしょうがないとします。
クライアントスクリプト
次のスクリプトを作成します。変数「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」ボタンを押下すると次のようにエラーメッセージが表示されます。
以上
Author And Source
この問題について(複数チェックボックスの中で1つの選択を必須にする), 我々は、より多くの情報をここで見つけました https://qiita.com/htshozawa/items/77b11cc197c047f13d21著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .