Jquery自动完成:如何设置要发送到服务器的内容?
目前,在输入内容后,服务器返回的内容如下:
[{"id":"1", "value":"My first character"},{"id":"2", "value":"My second Character"}]
当我选择“我的第一个角色”并将表格汇总时,它会发送:
array(1) { ["members"]=> string(18) "My first character" }
但我宁愿发送身份证 。 如何告诉.autocomplete发送id而不是字段的标签? ( JS代码 )
它必须简单,但我想念它。
谢谢
实际上,如果它是您正在使用的文本输入,您将始终提交显示的值。 你可以做的是将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; }
一种可能的解决方案是在表单中添加一个隐藏字段,用于存储当前选定的ID。
使用“自动完成” select
元素时,可以使用select
事件使用所选ID填充此隐藏字段。
从菜单中选择项目时触发; ui.item指的是所选项目
在自动填充数据中,添加label
字段,然后将value
字段更改为ID。