douzhengyi5022 2014-10-01 09:30
浏览 112
已采纳

获取引导下拉值OnSubmit

Dropdown made using 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 to keep dropdown option selected

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 Dropdown

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

For the above dropdown, I am fetching the option value based on user selection.

While doing it with Bootstrap, I am able to keep the the dropdown value selected, but as I am using it with PHP, I also need the value in $_POST variable in order to execute SQL query and display results based on what value is selected from dropdown.

How can I achieve it?

  • 写回答

1条回答 默认 最新

  • dongying7847 2014-10-01 09:42
    关注

    Use a hidden field and populate it when the user selects a state.

    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 Use inspect element to check the value of the hidden field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思