简体   繁体   中英

submit button get checkbox input safed in object jquery

I'm new to jquery and I have a question about how i can save the input from my checkboxes in an object when clicking on a button. For now i just want it to see it in the console. my code looks something like this:

 let UserInput = []; const addUserInput = (ev) => { ev.preventDefault(); let TextInput = { Name: document.getElementById('exampleInputName').value, //this is working InfoElektronik: $('#Info-Elektronik').click(function() { //this is not? console.log("Checkbox1 = " + $('#Info-Elektronik').prop('checked')); }); }; }; 
 <div class="form-check"> <input class="form-check-input" type="checkbox" name="agree" value="" id="Info-Elektronik"> <label class="form-check-label" for="defaultCheck1">Elektronik</label> </div> 

The issue is that InfoElektronik stores a function, not the actual return value. Also in your click function, you log the value to the console.

I would do it like this:

const TextInput = {
  Name: document.getElementById('exampleInputName').value
}

$('#Info-Elektronik').click(function() {
  textInput.InfoElektronik = ("Checkbox1 = " + $('#Info-Elektronik').prop('checked')).val();
})

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