mercredi 21 février 2018

How to create a string with checkbox?

I'm trying to get a result that the string will turns to "The person selected 1, 2, 3."

However with the code below, if I checked all three boxes I can only generate: The person selected 1, The person selected 1, 2, The person selected 1, 2, 3,

I don't want the generated notes have 3 separate lines.

        var notes = "";
        var reason = "";
        for (j=0; j<document.reason.reasonqst.length; j++){
            if (document.reason.reasonqst[j].checked==true){
                notes += "The person selected";
                if (j==0)
                    reason += "1, ";
                if (j==1)
                    reason += "2, ";
                if (j==2)
                    reason += "3,";
                notes += "" + reason + "\r";

            }
        }



    <form id="reason" name="reason" method="post" action="">
    <table>
        <tr>        
            <td>BPC: <input onclick="ds_sh(this);" name="reason" id="reason" readonly="readonly" style="cursor: text" /><br /></td>
        </tr>
        <tr>
            <td style="width: 120px;"><input type="checkbox" name="reasonqst" id="reasonqst" />1</td>
            <td style="width: 120px;"><input type="checkbox" name="reasonqst" id="reasonqst" />2</td>
            <td style="width: 120px;"><input type="checkbox" name="reasonqst" id="reasonqst" />3</td>

        </TR>
    </table>
         </form>




Aucun commentaire:

Enregistrer un commentaire