[英]how to get value of this option box and put it in javascript variable
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: 使用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>
Try this pure javascript solution (without jQuery) 试试这个纯JavaScript解决方案(不使用jQuery)
HTML (change your current SELECT html tag by - attribute an id) HTML(通过更改当前的SELECT html标签-属性ID)
<select id="myselect">
JAVASCRIPT JAVASCRIPT
var myvariable = null;
document.getElementById('myselect').onchange = function(e) {
myvariable = this.options[this.selectedIndex].value;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.