获取引导下拉值OnSubmit

时间:2014-10-01 09:30:30

标签: php jquery twitter-bootstrap drop-down-menu

使用Bootstrap

进行下拉列表
<form id="search" action="" method="post" >
    <div class="dropdown">
      <button data-toggle="dropdown" id="dropdownMenu1" type="button" class="btn btn-default dropdown-toggle">Fixed <span class="caret"></span></button>

      <ul aria-labelledby="dropdownMenu1" role="menu" class="dropdown-menu">
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Florida</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Texas</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Washington</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">New York</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Ohio</a></li>
          </ul>
    </div>
</form>

jQuery保持选择下拉选项

var jQee = jQuery.noConflict();
jQee(".dropdown-menu li").click(function(){
    var selText = jQee(this).text();
    jQee(this).parents('.dropdown').find('.dropdown-toggle').html(selText+' <span class="caret"></span>');
    //jQee('#search').submit();
});

HTML下拉列表

<select onchange="form.submit()" name="template">
    <option value="F" selected="true">Florida</option>
    <option value="T">Texas</option>
    <option value="W">Washington</option>
    <option value="N">New York</option>
    <option value="O">Ohio</option>
</select>

对于上面的下拉菜单,我根据用户选择提取option value

使用Bootstrap执行此操作时,我可以保留下拉值selected,但当我将其与PHP一起使用时,我还需要{{1}中的值}}变量,以便执行$_POST查询并根据从下拉列表中选择的值显示结果。

我怎样才能实现它?

1 个答案:

答案 0 :(得分:0)

使用隐藏字段并在用户选择状态时填充它。

HTML

<form id="search" action="" method="post" >
    <input type="hidden" name="selection">
    <div class="dropdown">
      <button data-toggle="dropdown" id="dropdownMenu1" type="button" class="btn btn-default dropdown-toggle">Fixed <span class="caret"></span></button>

      <ul aria-labelledby="dropdownMenu1" role="menu" class="dropdown-menu">
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Florida</a></li>
          <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Texas</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Washington</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">New York</a></li>
            <li role="presentation"><a href="#" tabindex="-1" role="menuitem">Ohio</a></li>
          </ul>
    </div>
</form>

的JavaScript

$(document).ready(function() {
    $("ul li a").click(function() {
        text = $(this).closest("li").text();
        $("input[name='selection']").val(text);
        $(this).parents('.dropdown').find('.dropdown-toggle').html(text+' <span class="caret"></span>');
        $("#search").submit();
    });
});

See jsfiddle使用inspect元素检查隐藏字段的值。