无法从选择选项中检索值

时间:2012-11-07 20:47:28

标签: javascript html

每当我尝试从下拉列表中更改值时,它就会重定向到其他URL ..

无论如何,我只是没有什么问题..

当我尝试更改价值时,我没有获得品牌价值,它显示为:'undefined'

任何想法?

  <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>

2 个答案:

答案 0 :(得分:0)

我很可能会选择显示类似的内容

<option value="<?php echo $brand; ?>"><?php echo $brand; ?></option>

然后获得Bokonic提到的选定值

sel.options[sel.selectedIndex].value

让我知道这是如何运作的。

答案 1 :(得分:0)

您无需更改生成代码。

您也不需要使用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/

相关问题