在php中获取bootstrap下拉列表的值 [英] Get values of bootstrap dropdown in php

查看:83
本文介绍了在php中获取bootstrap下拉列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含多个字段的表单,例如输入类型名称,复选框和下拉列表.我的下拉代码:

I have created a form with multiple fields like input type name, checkboxes and also a dropdown. My code for dropdown:

<div class="container">
<form action="selecttest.php" method="post">
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
  <ul class="dropdown-menu myclass numberom1" aria-labelledby="menu1" name="thenumbers">
      <li value="one">One</li>
      <li value="two">Two</li>
      <li value="three">Three</li>
      <li value="four">Four</li>
      <li value="five">Five</li>
  </ul>

</div>
<input type="submit" name ="submit"/>

我想在selecttest.php中获取所选<li>的值.以下代码不起作用:

I want to get the value of the selected <li> in the selecttest.php. The following code is not working:

$val = $_POST["thenumbers"];
    echo "the Value selected is ".$val;

如何在php页面中获取下拉菜单的值?如上所述,表单中还有其他元素,因此我无法使用jquery onclick事件.这是非ajax形式.

How can I get the value of the dropdown in the php page? There are other elements in the form as mentioned above so i cannot use jquery onclick event. And this is a non ajax form.

谢谢.

推荐答案

下拉菜单与select输入不同.

在一个表单中,select输入(例如在Bootstrap中的输入)的语法为:

Within a form, the syntax for a select input (like yours, within Bootstrap) would be:

<label for="select_1">Select list:</label>
<select class="form-control" id="select_1" name="thenumbers">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
    <option value="four">Four</option>
</select>

只要select具有值为的属性name,并且select在提交的表单内,您就可以获取PHP中所选元素的值.与:

So long as the select has an attribute name with value "thenumbers", and the select is within the form that is being submitted, you will be able to get the value of the selected element in PHP with:

$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;


要将Bootstrap下拉菜单用作伪装select,可以使用JavaScript.这是jQuery的示例:


To use the Bootstrap dropdown as a faux select, you can use JavaScript. Here is an example with jQuery:

HTML

<!-- Create dropdown and add specific class '.thenumbers' to ul element -->
<div class="dropdown">
    <div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
    <ul class="dropdown-menu myclass numberom1 thenumbers" aria-labelledby="menu1" name="thenumbers">
        <li value="one">One</li>
        <li value="two">Two</li>
        <li value="three">Three</li>
        <li value="four">Four</li>
        <li value="five">Five</li>
    </ul>
</div>

<!-- Create a hidden input -->
<input type='hidden' name='thenumbers'>

jQuery

$(function(){
    //Listen for a click on any of the dropdown items
    $(".thenumbers li").click(function(){
        //Get the value
        var value = $(this).attr("value");
        //Put the retrieved value into the hidden input
        $("input[name='thenumbers']").val(value);
    });
});

这样(只要隐藏的输入在表单内),提交表单时,"thenumbers"输入值将是最后选择的下拉选项.

That way (as long as the hidden input is within the form), when the form is submitted, the "thenumbers" input value will be the last selected dropdown option.

这篇关于在php中获取bootstrap下拉列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆