问题描述:

I have a page layout like the below and I need to set values in <select> as I have a form.

<form action="add.jsp">

<input name="title" id="title"/>

<input name="company" id="company" />

<select name="options" id="options">

</select>

<button>Submit</button>

</form>

I want to add values to above <select/> from the checkboxes out side of the form which are

<input id="input-checkbox-1" name="input-checkbox" type="checkbox">Option 1

<input id="input-checkbox-2" name="input-checkbox" type="checkbox">Option 2

<input id="input-checkbox-3" name="input-checkbox" type="checkbox">Option 3

now don't have any idea of how to get values from checkboxes and add options to select using jQuery.

网友答案:

now don't have any idea of how to get values from checkboxes and add options to select using jQuery.

First, to get an HTML element, you can use $("css selector"), document.getElementById("id value"), or document.querySelector("css selector").

Here's a CSS selector syntax reference: http://www.w3.org/TR/selectors/#selectors

Next, to retrieve the value of a checkbox, you can refer to the .checked property of the element. It will be true if the box is checked and false otherwise.

document.querySelector("input").addEventListener("change",function(){
  alert(this.checked);
});
<input type="checkbox" />Check me
相关阅读:
Top