由LaQuery中的Controller无法识别的JQuery创建的表单元素

时间:2015-12-11 07:47:07

标签: php jquery laravel

下午好,

我一直试图让它工作一段时间,但似乎无法做到。我有一个表格的视图。在该表单中有一个选择下拉列表和一组输入,用于将信息传递给Controller进行处理。

问题是,表单需要能够通过添加或删除新输入来改变自身,具体取决于在select中选择的选项。

我所做的是使用JQuery追加和删除元素。它工作正常,但控制器无法识别以这种方式创建的元素。请看一下:

控制器

    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>

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

谢谢你们,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

您尚未将请求与控制器功能实际连接。 喜欢描述here。 将控制器功能更改为此,您现在应该能够访问该变量。

public function userface2show(Request $request) {

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

答案 1 :(得分:1)

下午好好偷看,

你永远不会相信解决这个问题的原因。我和其他一些开发人员看了一下这个问题并且无法说出错误,所以我们开始逐个删除元素以查看导致问题的原因。

当我们删除HTML <table>格式时,它开始工作。 <tr><td>正在干扰前端将信息传递到后端的能力。

谢谢大家查看问题。

相关问题