目前,在输入内容后,服务器会返回如下内容:
[{"id":"1", "value":"My first character"},{"id":"2", "value":"My second Character"}]
当我选择“我的第一个 Angular 色”并提交表格时,它会发送:
array(1) { ["members"]=> string(18) "My first character" }
但我宁愿发送 ID。如何告诉 .autocomplete 发送 id 而不是字段的标签? ( JS code )
它一定很简单,但我想念它。
谢谢
最佳答案
实际上,如果您使用的是文本输入,您将始终提交显示的值。您可以做的是将 id 复制到隐藏元素,然后捕获该服务器端。你可以这样做:
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
//get the id
var id = ui.term.id;
//copy it to another hidden element
$('#yourhiddeninput').val(id);
return false;
}
关于javascript - Jquery 自动完成 : how to setup what to send to the server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7486741/