下午好
一段时间以来,我一直在努力让它发挥作用,但似乎做不到。我有一个表单 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/