jQuery 当我 select 一个调用函数的选项
jQuery When I select an Option to call a function
当我 select 从我的数据库中得到一个结果时,我想调用一个函数,用相应的信息填充字段 "Linha",如下图所示:
这是我的代码:
我的自动完成:
$("#produto").autocomplete({
source: '/pedidoOnline/index.php/Pedidos/search',
minLength: 2,
focus: function(event, ui) {
$("#produto").val(ui.item.label);
return false;
},
select: function(event, ui) {
$('#procura_produto').val(ui.item.id);
}
});
我要调用的填充字段的函数"Linha":
function preencherLinhaProduto(obj) {
$("#buscaLinha").autocomplete({
source: '/pedidoOnline/index.php/Pedidos/pesquisarLinhaProduto/' + $('#procura_produto').val(),
minLength: 2,
select: function(event, ui) {
$(obj).each(function() {
$(this).closest('tr').find('input.cod_linha').val(ui.item.id);
$(this).closest('tr').find('input.linha').val(ui.item.value);
});
}
});
}
我的HTML:
<div>
<input type="button" value="Produtos" class="btn btn-success" onClick="dialogProcurarProdutos()">
<div id="dialogProdutos" title="Procurar produtos cadastrados">
<label for="produto">Informe o produto que deseja procurar</label>
<input required type="hidden" name="procura_produto" id="procura_produto"/>
<input required class="inputGG form-control" type="text" name="produto" id="produto" placeholder="Digite no mÃnimo as duas letras iniciais"/>"
</div>
以及搜索字段"Linha"
public function pesquisarLinhaProduto() {
$this->autoRender = false;
$this->loadModel('ProcuraProdutoPedOnline');
// Consultando pelo que o usuário está digitando
$produto = $this->request->params['pass'][0];
$linhas = $this->ProcuraProdutoPedOnline->find('all', array(
'fields' => array('cd_linha', 'ds_linha'),
'conditions' => array(
'cd_cpl_tamanho' => "$produto",
)
));
// Formatar resultado
$result = array();
foreach ($linhas as $key => $linha) {
$result[$key]['id'] = (int) $linha['ProcuraProdutoPedOnline']['cd_linha'];
$result[$key]['label'] = utf8_encode($linha['ProcuraProdutoPedOnline']['ds_linha']);
}
$linhas = $result;
echo json_encode($linhas);
}
但不幸的是,这不起作用。谁能帮忙?
你快到了!
您需要从自动完成的 'select' 回调内部调用回调函数(在本例中看起来是 preencherLinhaProduto)。
[这个场景的小 jsfiddle:http://jsfiddle.net/xngLuczn/ 在这里]
select: function (event, ui) {
console.log(ui.item); //just to check
preencherLinhaProduto(ui.item.value); //send the value here as parameter
//so the function can be generic
}
如果您在这样做时遇到错误,请post 相关错误日志以获得更有帮助的答案。
此外,我建议您将 select 回调和实际的自动完成药水分开(不使用匿名函数),以使其保持通用、干净且易于您自己理解和根据需要进行增强。
在@suvartheec 的帮助下:
select: function (event, ui) {
console.log(ui.item); //just to check
preencherLinhaProduto(ui.item.value); //send the value here as parameter
//so the function can be generic
}
成功了,非常感谢。
当我 select 从我的数据库中得到一个结果时,我想调用一个函数,用相应的信息填充字段 "Linha",如下图所示:
这是我的代码:
我的自动完成:
$("#produto").autocomplete({
source: '/pedidoOnline/index.php/Pedidos/search',
minLength: 2,
focus: function(event, ui) {
$("#produto").val(ui.item.label);
return false;
},
select: function(event, ui) {
$('#procura_produto').val(ui.item.id);
}
});
我要调用的填充字段的函数"Linha":
function preencherLinhaProduto(obj) {
$("#buscaLinha").autocomplete({
source: '/pedidoOnline/index.php/Pedidos/pesquisarLinhaProduto/' + $('#procura_produto').val(),
minLength: 2,
select: function(event, ui) {
$(obj).each(function() {
$(this).closest('tr').find('input.cod_linha').val(ui.item.id);
$(this).closest('tr').find('input.linha').val(ui.item.value);
});
}
});
}
我的HTML:
<div>
<input type="button" value="Produtos" class="btn btn-success" onClick="dialogProcurarProdutos()">
<div id="dialogProdutos" title="Procurar produtos cadastrados">
<label for="produto">Informe o produto que deseja procurar</label>
<input required type="hidden" name="procura_produto" id="procura_produto"/>
<input required class="inputGG form-control" type="text" name="produto" id="produto" placeholder="Digite no mÃnimo as duas letras iniciais"/>"
</div>
以及搜索字段"Linha"
public function pesquisarLinhaProduto() {
$this->autoRender = false;
$this->loadModel('ProcuraProdutoPedOnline');
// Consultando pelo que o usuário está digitando
$produto = $this->request->params['pass'][0];
$linhas = $this->ProcuraProdutoPedOnline->find('all', array(
'fields' => array('cd_linha', 'ds_linha'),
'conditions' => array(
'cd_cpl_tamanho' => "$produto",
)
));
// Formatar resultado
$result = array();
foreach ($linhas as $key => $linha) {
$result[$key]['id'] = (int) $linha['ProcuraProdutoPedOnline']['cd_linha'];
$result[$key]['label'] = utf8_encode($linha['ProcuraProdutoPedOnline']['ds_linha']);
}
$linhas = $result;
echo json_encode($linhas);
}
但不幸的是,这不起作用。谁能帮忙?
你快到了! 您需要从自动完成的 'select' 回调内部调用回调函数(在本例中看起来是 preencherLinhaProduto)。 [这个场景的小 jsfiddle:http://jsfiddle.net/xngLuczn/ 在这里]
select: function (event, ui) {
console.log(ui.item); //just to check
preencherLinhaProduto(ui.item.value); //send the value here as parameter
//so the function can be generic
}
如果您在这样做时遇到错误,请post 相关错误日志以获得更有帮助的答案。
此外,我建议您将 select 回调和实际的自动完成药水分开(不使用匿名函数),以使其保持通用、干净且易于您自己理解和根据需要进行增强。
在@suvartheec 的帮助下:
select: function (event, ui) { console.log(ui.item); //just to check preencherLinhaProduto(ui.item.value); //send the value here as parameter //so the function can be generic }
成功了,非常感谢。