Javascript Form Submission After Validation

前端 未结 4 922
花落未央
花落未央 2021-01-17 05:35

This script functions mostly how I would like it to: alert when a radio checkbox has not been selected. However, if all buttons are selected I need it the form to be submitt

4条回答
  •  猫巷女王i
    2021-01-17 05:46

    You're returning false regardless of your validation. Change the end of your code from:

    if(treatmentChoice == "") {
        alertMsg += "Treatment" + "\n"
    } {
          alert(alertMsg)
    };
    return false;
    document.forms["form"].submit();
    

    to:

    if(treatmentChoice == "") {
        alertMsg += "Treatment" + "\n"
    }
    if(alertMsg.length > 16) {
        alert(alertMsg);
        return false;
    } else {
        document.forms["form"].submit();
    }
    

    The length check checks the final value length of alertMsg against what you originally set it to.

提交回复
热议问题