简体   繁体   中英

HTML checkbox to Javascript form

I am (still) working on creating sidebar input for a Sheets add-on I'm developing. My current challenge is allowing a textbox and a checkbox to be passed from the form to my function.

The textbox works and successfully passes the variable. The checkbox I'm not as clear on. I need to turn the checkbox into a boolean.

The relevant html:

 <p> <input type="text" id="datepicker"> </p> <p> Include pledge?<br> <input type="checkbox" name="pledge[]" id="pledgeCheck" value="true">Yes<br> </p> <p> <input type="button" name="createdate" value="Create" onClick="google.script.run.create(document.getElementById('datepicker').value,document.getElementById('pledgeCheck').value)" /> </p> 

 function create(datepicker,pledge) { //important vars var responses = ss.getSheetByName("Form Responses"); var bulletin = ss.getSheetByName(todayDate); var todayDate=datepicker; var pledgeCheck=pledge; Logger.log(pledgeCheck); 

currently the pledge feeds back as undefined.

SOLVED: A few things I wasn't getting, and maybe a few I just sort of overlooked and missed. I didn't have the right terms in all the right places. @umair pointed the way in my html, as shown below. My js, however, used pledge instead of pledgeCheck in the arguments.

  1. Remove the value from checkbox, otherwise it will be always true.

  2. Check whether the checkbox is checked or not with .checked attribute and not with .value ; this will return true for checked and false for unchecked.

 <p> <input type="text" id="datepicker"> </p> <p> Include pledge?<br> <input type="checkbox" name="pledge" id="pledgeCheck">Yes<br> </p> <p> <input type="button" name="createdate" value="Create" onClick="google.script.run.create(document.getElementById('datepicker').value,document.getElementById('pledgeCheck').checked)" /> </p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM