提交时无法获取引导程序下拉列表的值

我有以下代码,当我单击提交按钮时,我想获取引导下拉按钮上显示的文本的值,并且我还希望将 POST 发送到 codeigniter 控制器以对其执行某些操作. 我需要帮助。


    <form method="post" action="#" id="user-order-form">

      <div class="users-order col-md-5">

        <label class=""> <strong>ORDER BY </strong></label>

        <div class="dropdown form-control" style="padding:0px; margin-left: 20px; margin-right:10px">

          <button class="btn btn-default dropdown-toggle form-control" type="button" id="user-order-dropdown" name="user-order-dropdown" data-toggle="dropdown">

            Surname

            <span class="caret"></span>

          </button>

          <ul class="dropdown-menu" aria-labelled-by="user-order-dropdown" id="capton">

            <li><a href="#">Surname </a> </li>

            <li><a href="#">First Name </a> </li>

            <li><a href="#">Company Name </a> </li>

          </ul>

        </div>

        <label class="radio-inline"><input type="radio" name="order-radio" checked>ASC</label>

        <label class="radio-inline"><input type="radio" name="order-radio">DESC</label>



        <button type="submit" class="btn" name="order-submit-btn" id="order-submit-btn">Order</button>

      </div>

    </form>

这是显示所选下拉项的 jquery 代码:


    $('.dropdown').each(function (key, dropdown) {

       var $dropdown = $(dropdown);

       $dropdown.find('.dropdown-menu a').on('click', function () {

          $dropdown.find('button').text($(this).text()).append(' <span class="caret"></span>');

       });

    });


慕神8447489
浏览 87回答 1
1回答

慕妹3242003

要获取下拉列表的值:您需要创建一个隐藏的输入字段,并且每次下拉列表的值发生变化时,您都需要更新它。要获取POST中的数据controller:您需要在该控制器中创建一个控制器(显然)和一个函数。然后将该 URL 提供给表单操作属性。我已经为你写了一个参考代码,并在评论中进行了必要的解释,看看它是否对你有帮助。看法<form method="post" action="<?php echo base_url('some_controller/some_function');?>" id="user-order-form"><!-- give the action where controller name(some_controller) and function name(some_function) -->&nbsp; <div class="users-order col-md-5">&nbsp; &nbsp; <label class=""> <strong>ORDER BY </strong></label>&nbsp; &nbsp; <div class="dropdown form-control" style="padding:0px; margin-left: 20px; margin-right:10px">&nbsp; &nbsp; &nbsp; <button class="btn btn-default dropdown-toggle form-control" type="button" id="user-order-dropdown" name="user-order-dropdown" data-toggle="dropdown">&nbsp; &nbsp; &nbsp; &nbsp; Surname&nbsp; &nbsp; &nbsp; &nbsp; <span class="caret"></span>&nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; <!-- make a hidden field, give it a default value(which is initially selected), give it a name(it will be used to get the post data)-->&nbsp; &nbsp; &nbsp; <input type="hidden" name="dropdown" value="Surname"id="dropdown_input"/>&nbsp; &nbsp; &nbsp; <ul class="dropdown-menu" aria-labelled-by="user-order-dropdown" id="capton">&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="#">Surname </a> </li>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="#">First Name </a> </li>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="#">Company Name </a> </li>&nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </div>&nbsp; &nbsp; <label class="radio-inline"><input type="radio" name="order-radio" checked>ASC</label>&nbsp; &nbsp; <label class="radio-inline"><input type="radio" name="order-radio">DESC</label>&nbsp; &nbsp; <button type="submit" class="btn" name="order-submit-btn" id="order-submit-btn">Order</button>&nbsp; </div></form>查询&nbsp; &nbsp;$('.dropdown').each(function (key, dropdown) {&nbsp; &nbsp; &nbsp; &nbsp;var $dropdown = $(dropdown);&nbsp; &nbsp; &nbsp; &nbsp;$dropdown.find('.dropdown-menu a').on('click', function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dropdown.find('button').text($(this).text()).append(' <span class="caret"></span>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#dropdown_input').val($(this).text()); // change the value of hidden input field&nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; });控制器(Some_controller.php)function some_function(){&nbsp; &nbsp; $dropdown&nbsp; &nbsp; = $this->input->post('dropdown'); // get dropdown value&nbsp; &nbsp; $order_radio = $this->input->post('order-radio'); // get radio value&nbsp; &nbsp; /* Do whatever you want to with that data here(Eg - Save in DB) -- your logic */}
打开App,查看更多内容
随时随地看视频慕课网APP