简体   繁体   中英

How to extract content from checkbox in javascript?

I'm using a function like this to extract a few data from a form:

$(document).ready(function() { 
     $('#btnEnviar').click(function() {
            var email = $('#email').val();

It's working, but now I also need to extract the content of a list box, which is a mark sign.

So I put the list box inside a span with id "idaevolta":

      <ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul>
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul>

 <input id="cbIdaVolta" type="hidden" name="cbIdaVolta" />

So far so good, but when I try to extract the data:

var idaevolta = $("#idaevolta").html();

It is simply not working.

UPDATE

WHILE LOOKING AT THE HTML OUPUT, WITH CHROME CONSOLE, LOOK WHAT I FOUND:

<input type="checkbox" id="cbIdaVolta" name="cbIdaVolta" style="width: 35px; height: 35px; margin-left: 10px;">

Does it make things easier to find a solution?

UDPATE 2

It is actually a check box.

You have multiple elements with the ID "idaevolta" ... the span and the input elements. ID attributes must be unique.

First of all, you can never set two different items with the same id. Only the first one will be recognized. Second, I would use some jquery to say 'if 1st ul is clicked, set #idaevolta value to something'. After this, you'd retrieve the value just like you've done with the email field.

https://api.jquery.com/click/

I hope that helps!

PS: having 'ul's without any 'li' in it just look weird... maybe you should try something different

  1. you can only have one ID in the document, even if they are different elements
  2. since you break that rule, you can $('span').html()

You should have a unique name for id , otherwise use class . ul element should have li . And if you want to get the data from a list. Try something like this:

var x = $('#idaevolta').find('ul li').text();

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