JS Limit the Number of Checked Options in Checkbox Group

  • Last update:  2020-12-30
  • I. Overview

    1.1 Problem description

    When making a form, sometimes it is required to limit the maximum number of options selected in a checkbox group. For example, set the maximum as 2, and if more than 2 options are selected, all selected values will be cleared or only the part that is beyond the limitation will be cleared.

     

    1.2 Implementation ideas

    Get the value of the current cell through JS, and then use the length property to determine the number of selected options and perform operations.

    II. Example

    2.1 Operation steps

    Add a checkbox group in cell A1, and the data dictionary is customized as A, B, C and D, as shown below:

    1.png


    2.2 Clear all selected options

    1) Add JS event

    Add a State Change event to the checkbox group, as shown below:

    2.png

    The JS code is as follows:

    var value = this.getValue(); //Get the value of the current widget
    if(value.length>2){ //Determine whether the length of widget value exceeds 2
    alert("length is "+value.length+", more than the maximun length 2!");
    this.reset(); //Reset the data, that is, clear the selected options
    }

    Note: You can also use this.setValue(""); to clear the data.

    2) View the effect

    Click Data Entry Preview, check 3 options, and message "length is 3, more than the maximum length 2!" will pop up. After clicking OK, all the selected options are cleared.

    1.gif


    2.3 Only clear the options that are beyond limitation

    1) Modify the above JS

    3.png

    The JS code is modified to:

    var value = this.getValue();
    var oldvalue = contentPane.curLGP.getCellValue("A1")
    if(value.length>2){
    alert("length is "+value.length+", more than the maximun length 2!");
    this.setValue(oldvalue);
    }

    2) View the effect

    Click Data Entry Preview, select 3 options in the checkbox group, and message "length is 3, more than the maximum length 2!" will pop up. After clicking OK, only the third selected option is cleared.

    2.gif


    2.4 Preview effect on mobile terminal

    Clear all selected options:

    3.gif

    Note: Clearing the options that are beyond the limitation is not applicable on mobile terminal.

    III. Template download

    Attachment List


    Theme: Data Entry
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy