php - Laravel 中的 Controller 无法识别由 JQuery 创建的表单元素

标签 php jquery laravel

下午好

一段时间以来,我一直在努力让它发挥作用,但似乎做不到。我有一个表单 View 。在该表单中是一个选择下拉列表和一堆输入,这些输入将信息传递给 Controller ​​进行处理。

问题是,表单需要能够根据在 select 中选择的选项添加或删除新输入来更改自身。

我所做的是使用 JQuery 来添加和删除元素。它在前端运行良好,但 Controller 无法识别以这种方式创建的元素。请看一下:

Controller

    public function userface2show() {

    $keymaker = Request::get('Search');

    dd(Request::all()); 

//this works only on the original elements in the view, any element created by my JQuery does not reflect in the dd(Request::all());

}

查看:

    <script>

    $(document).ready(function(){

    $(function(){


       $('#specialselect').change(function() {

        if($("#specialselect option:selected" ).text() == "Year") { // activates the change when option from the select is activated

            $("#selectornode").remove(); // removes original element
            $("#selectorbox").append("<input id='selectornode1' name='Search1' type='text'>");  // adds new element with another "name"

        } 

        else {

            $("#selectornode1").remove(); // removes new element
            $("#selectorbox").append("<input id='selectornode' name='Search' type='text'>"); // creates replicate of the original element. This replica does not work either.
        }

       });

    });

    });

    </script>

            <table class="formstyle">
            {!! Form::open(array('action' => 'MasterController@userface2show')) !!}
            <tr>
            <td>
            {!! Form::label("Select Area/Field of Study") !!}
            </td>
            <td>
            {!! Form::select('Area', $Area) !!}
            </td>
            <td>
            {!! Form::label("Specify Search Parameter") !!}
            </td>
            <td>
            {!! Form::select('Parameters', $Parameters, 'default', array('id' => 'specialselect')) !!}
            </td>
            <td>
            {!! Form::label("Input Word to Search", null, array('id' => 'selectortext')) !!}
            </td>
            <td id="selectorbox">
            {!! Form::text('Search', null, array('id' => 'selectornode')) !!} // this is the only element that needs to be changed.
            </td>
            <td>
            {!! Form::submit('Go', ['class' => 'buttonite']) !!}
            </td>
            </tr>
            {!! Form::close() !!}
            </table>

有没有办法让这些新创建的表单输入起作用?

谢谢大家,我们将不胜感激。

最佳答案

您还没有实际将请求连接到您的 Controller 功能。 喜欢描述here . 将您的 Controller 功能更改为此,您现在应该能够访问该变量。

public function userface2show(Request $request) {

    $keymaker = $request->input('Search');
    dd($request);
}

关于php - Laravel 中的 Controller 无法识别由 JQuery 创建的表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34218486/

相关文章:

php - 修改PHP代码SocialEngine

jquery - 如何检测Android上的后退和菜单按钮? PhoneGap + jQuery 移动

jquery - 包含一系列面板的一页网站,每个面板的大小都是视口(viewport)的大小

mysql - SQL 查询未给出所需结果

laravel - 2 模型 2 foreach 在 1 个 View 中。拉维尔

php - 将数据库部署到远程服务器后,我无法将新记录插入到数据库中

php - 检查日期是工作日还是周末?

javascript - 如何以 Angular 使用functions.php文件中的函数

javascript - 如何为没有内容的单个选项卡着色?

php - 使用 Laravel 5.4 从数据库中删除数据