[英]Unable to retrive value from select options
每当我尝试从下拉列表更改值时,它都会重定向到其他URL。
无论如何,我只是没有什么问题。
当我尝试更改价值时,我没有获得品牌价值,它向我显示:“未定义”
任何想法?
<select id="brand" onchange="gotoPage()">
<?php
$brands = dfr_get_brands_list($category);
foreach ($brands as $brand) : ?>
<option><?php echo $brand; ?></option>
<?php endforeach; ?>
</select>
<?php if (@$_GET['brand']) { ?>
<a href="[server.url type='fullpage' query='-brand']"><FONT COLOR="red"><?php echo"[[X] Remove Brand "; ?><?php echo @$_GET['brand']; ?></font></a>
<?php } ?>
<script>
function gotoPage(){
var url = "http://laptopsisland.com/shop/c/laptops/";
var sel = document.getElementById("brand");
var brand = sel.options[sel.selectedIndex].text;
alert(brand);
window.location.href = url + "&brand=" + brand;
}
</script>
我将有机会选择显示类似这样的内容
<option value="<?php echo $brand; ?>"><?php echo $brand; ?></option>
然后获得Bokonic提到的选定值
sel.options[sel.selectedIndex].value
让我知道它是如何工作的。
您无需更改生成代码。
您也不需要使用getElementById。
<script>
function gotoPage(e) {
var brand = e.options[e.selectedIndex].text;
alert(brand);
}
</script>
<select id="brand" onchange="gotoPage(this)">
<option>aaa</option>
<option>bbb</option>
</select>
工作解决方案: http : //jsfiddle.net/2YzAV/2/
带有值的版本: http : //jsfiddle.net/2YzAV/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.