Fetch Bootstrap Dropdown Value OnSubmit

2019-05-18 07:18发布

问题:

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:

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.