0

这是我的代码,我想获取所选选项的值,但是如何获取?

<?php
    $sql = "SELECT * FROM `category`";
    $category = mysql_query($sql); ?>
        <select>
            <?php
            while ($row = mysql_fetch_array($category)) {
            echo "<option id='cat' value='" . $row['catid'] ."'>" . $row['catname'] ."</option>";
            }
            ?>
        </select>
4

2 回答 2

0

使用 jQuery( http://api.jquery.com/selected-selector/ ),假设你有:

<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option selected="selected">Shrubs</option>
  <option>Trees</option>
  <option selected="selected">Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>

<script>
$( "select" )
  .change(function() {
    var str = "";
    $( "select option:selected" ).each(function() {
      str += $( this ).text() + " ";
    });
    $( "div" ).text( str );
  })
  .trigger( "change" );
</script>
于 2013-09-29T16:38:43.187 回答
0

试试这个纯 javascript 解决方案(没有 jQuery)

HTML(通过 - 属性 id 更改您当前的 SELECT html 标记)

<select id="myselect">

JAVASCRIPT

var myvariable = null;
document.getElementById('myselect').onchange = function(e) {
    myvariable = this.options[this.selectedIndex].value;
}​;
于 2013-09-29T16:40:23.047 回答