This is my code i want to get the value of the selected option but how?
<?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>
Use jQuery( http://api.jquery.com/selected-selector/ ), say you have:
<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>
Try this pure javascript solution (without jQuery)
HTML (change your current SELECT html tag by - attribute an id)
<select id="myselect">
JAVASCRIPT
var myvariable = null;
document.getElementById('myselect').onchange = function(e) {
myvariable = this.options[this.selectedIndex].value;
};
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.