我正在使用MVC框架cakephp,我需要恢复输入中写入的每个值,即使当我添加字符或删除一个字符时(你知道autocpmlete的目的),我必须恢复输入中的这个值以发送它用于生成包含建议者列表的 json 文件的操作。
这是我的函数 JavaScript:
$(document).ready(function () {
var autotext;
$('#searchItem').on("input",function() {
autotext = $('#divResult').html($(this).val());
console.log(autotext);
});
$('#searchItem').autocomplete({
data: { mot:autotext },
source: 'getAutocomplete.json',
dataType: "json",
minLength: 2
});
});
当我获取值时,从输入发送数据以在我的 Controller 中使用它:
$forsearch=$this->request->data['mot'];
我收到此错误,索引“mot”未定义。 如何获取 Controller 中的输入值?
最佳答案
这是用于在每次用户键入内容时获取输入值的代码。然后发送给 Controller 。
$(document).ready(function(){
$("#input").keyup(function(){
console.log($(this).val());
$.ajax({
url: "controller",
type: "get",
data:{text: $(this).val()},
success: function(response) {
//here is where you treat the JSON
},
error: function(xhr) {
//if stuff goes bad
}
});
});
});
“#input”是输入字段的 ID。
我正在使用ajax将输入值发送到 Controller 。
关于javascript - 获取输入中的值以进行自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39870630/