2014-09-30 77 views
0

價值我想從jQuery的獲取HTML從jQuery的

拿到$類別[$類]的價值,這是我的HTML

<select name='category' id='category'> 
         <option value='0'>Please select</option> 
         <?php 
         $param = array(); 
         $categories = $core -> getProductsCategories($param); 
         if($categories) { 
          foreach($categories as $category) { 
         print "<option value='$category[id]'>$category[category]</option>"; 
          } 
         } 
         ?> 
        </select> 
<input type="button" id="add" name="add" value="Add to sales order" />   
      <div class="messages"></div> 

這是我的jQuery我想要得到的值該陣列

$(document).ready(function() { 
 
    $('#add').click(function() { 
 
    //var toAdd = $("select[name=category]").val(); 
 
     var toAddd = $('select[name="category"]').val(); 
 
    // $('#messages1').append("<p>"+toAdd+"</p>"); 
 
     $('.messages').append("<p>"+toAddd+"</p>"); 
 
     
 
    }); 
 
});

+0

你指的是通過你的PHP腳本填充所有的''

回答

0

你可以試試這個演示

$(function() { 
 
    var ctg = $('#category'); 
 
    $('#add').on('click', function() { 
 
    console.log('Selected category value', ctg.val()); 
 
    console.log('Selected category text', ctg.find(':selected').text()); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select name='category' id='category'> 
 
    <option value='0'>Please select</option> 
 
    <option value='1'>Category 1</option> 
 
    <option value='2'>Category 2</option> 
 
</select> 
 
<input type="button" id="add" name="add" value="Add to sales order" /> 
 
<br/>Open console ...