如何使用php从下拉菜单中获取所选项目的值

时间:2016-04-25 08:41:03

标签: php

我有一点问题。如果您按顺序选择相同的值(或者如果在提交按钮上单击两次),则不会发生任何事情。但是,如果您从下拉列表中更改项目,那么它的工作正常。变量“var value = $(this).data(”value“);”的值如果您不从列表中选择其他项目,则不会更改。为什么我要更改值,因为此变量值从数据库获取行。我已经尝试过change()函数而没有任何成功。

      <div class="col-md-6 center">
    <H2>Macig Box</H2>
    Please Select Nummer Of Match.
    <form id="myform" method="get">
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true">
            Succes
            <span class="caret"></span>
        </button>

        <input type="hidden" id="selectedNummer" name="selectedNummer" />
        <ul class="dropdown-menu myCustomDropMenu" aria-labelledby="dropdownMenu1" onchange="change()" >

            <li><a href="#" data-value="1">1</a></li>
            <li><a href="#" data-value="2">2</a></li>
            <li><a href="#" data-value="3">3</a></li>
            <li><a href="#" data-value="4">4</a></li>
            <li><a href="#" data-value="5">5</a></li>
            <li><a href="#" data-value="6">6</a></li>
            <li><a href="#" data-value="7">7</a></li>
            <li><a href="#" data-value="8">8</a></li>

        </ul>
    </div>
        <input type="submit" value="Send">
</form>

        <script>
       $('.myCustomDropMenu li a').click(function(e){
     var value=$(this).data("value");
var selText = $(this).text();
$(this).parents('.dropdown').find('.dropdown-toggle').html(selText+'<span class="caret"></span>');
     $('#selectedNummer').val(value);
});
     </script>
     <script>
function change(){
    document.getElementById("myform").submit();
}
</script>

有人可以帮我归档这个目标。谢谢。

1 个答案:

答案 0 :(得分:2)

You've use jQuery to set the selected value from Dropdown to input field.

What you can do is, put a click trigger on <a> of dropdown menu, and on click of a <a> from dropdown set the corresponding value to selectedNummer input.

If there are multiple dropDownMenus on the page you can add a custom class to dropDown menu to put a specific click trigger.

So your HTML would look like this.

<div class="col-md-6 pull-right">
    <H2>Macig Box</H2>
    Please Select Nummer Of Match.
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true">
            Succes
            <span class="caret"></span>
        </button>

        <input type="hidden" id="selectedNummer" name="selectedNummer" />
        <ul class="dropdown-menu myCustomDropMenu" aria-labelledby="dropdownMenu1">
            <li><a href="#" data-value="1">1</a></li>
            <li><a href="#" data-value="2">2</a></li>
            <li><a href="#" data-value="3">3</a></li>
            <li><a href="#" data-value="4">4</a></li>
            <li><a href="#" data-value="5">5</a></li>
            <li><a href="#" data-value="6">6</a></li>
            <li><a href="#" data-value="7">7</a></li>
            <li><a href="#" data-value="8">8</a></li>
        </ul>
    </div>

And to set the value to selectedNummer input, JS would be

$('.myCustomDropMenu li a').click(function(e){
     var value=$(this).data("value");
     $('#selectedNummer').val(value);
});

Now when you submit the form using POST or GET method, you can access the value using $_POST['selectedNummer'] or $_GET['selectedNummer'], and use that to assign value to PHP variable.

//e.g.
$variableValue=$_POST['selectedNummer'];
// OR
$variableValue=$_GET['selectedNummer'];

Working jsFiddle for setting the input value: https://jsfiddle.net/1dLoqe44/