views:

55

answers:

4

I have five ul lists that I am using and each list might have an input in it.

Here is an example:

<form id="testForm">
<ul id="1">
     <li>TEST</li>
     <li>Gender: 
          <select id="gender" name="gender">
               <option value="0">Male</option>
               <option value="2">Female</option>
          </select>
     </li>
</ul>
<ul id="2">
     <li>TEST2</li>
     <li>Gender2: 
          <select id="gender2" name="gender">
               <option value="0">Male</option>
               <option value="2">Female</option>
          </select>
     </li>
</ul>
</form>

What I am trying to do is get an array of the li text and if there is an input get the selected value of it.

With jquery I have tried this:

$('#testForm').submit(function() {
     var optionTexts = [];
     for (i = 1; i < 2; i++) {
          $("#"+i).each(function() { optionTexts.push($(this).text()) });
     }
     alert(optionTexts);
     return false;
});

Now the problem that I am having is it gives me a list of all the UL LI but it is also giving all the options not the selected option. How would I go about doing this?

Edit

I forgot to add the output that I am looking for. I want an output like this:

[1, TEST1, gender => male]
[2, TEST2, gender => female]
A: 

to get the selected value of the select you can use $('#gender').val()

lakhlaniprashant.blogspot.com
A: 

You could use .val(), for example:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="language" content="en" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
</head>
<body>


<form id="testForm">
    <ul id="1">
         <li>TEST</li>
         <li>Gender: 
              <select id="gender" name="gender">
                   <option value="0">Male</option>
                   <option value="2">Female</option>
              </select>
         </li>
    </ul>
    <ul id="2">
         <li>TEST2</li>
         <li>Gender2: 
              <select id="gender2" name="gender">
                   <option value="0">Male</option>
                   <option value="2">Female</option>
              </select>
         </li>
    </ul>
    <input type="submit" value="submit" />
</form>


<script>
jQuery(document).ready(function($) { 

    $('#testForm').submit(function(){
        $(this).find('select').each(function(){
            alert( $(this).val() );
        });
         return false;
    });

});
</script>

</body>
</html>
wildpeaks
A: 

Maybe this will move you in the right direction a bit:

$('#testForm').submit(function() {    
   var optionTexts = [];
   $('ul li').each(function(){
        var $select = $(this).find('select');
        if($select.length)
           optionTexts.push($select.val());

    });   
    return false;
});

You can play with it here --> http://jsfiddle.net/jamiec/tThWZ/

Jamiec
A: 

Working on jamiec 's fiddle you can also create a multi dimesional array of the list items

$('#testForm').submit(function() {     
   var optionTexts = [];
   $('ul li',this).each(function(){
        var $select = $(this).find('select');
        if($select.length)
           optionTexts[optionTexts.length-1].push($select.val());
        else optionTexts.push(new Array($(this).text()));
    });
    return false;
});

(http://jsfiddle.net/tThWZ/1/)

Edit

Try the updated version. For some reason the associate array keys aren't working for me, sorry.

$('#testForm').submit(function() {     
   var optionTexts = [];
    count = 0;
   $('ul li',this).each(function(i){
        var $select = $(this).find('select');
        if($select.length)
           optionTexts[optionTexts.length-1].push($select.attr('name') => $select.val());
       else {
           count++;
           optionTexts.push(new Array(count,$(this).text()));
       }
    });
    if (typeof console != 'undefined')
        console.log(optionTexts);
    else alert(optionTexts);
    return false;
});

(http://jsfiddle.net/tThWZ/3/)

BenWells