如何将数组作为隐藏字段传递?
我正在使用 scriptaculous.js 框架的 Ajax.autocompleter 方法实现自动完成框。
这是自动完成框和填充自动建议条目的 div。
<?php echo $form->create('Share', array('url' => '/forms/share')); ?>
<label for="shareWith">Share Form with</label>
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" class="autocomplete"></div>
<input type="hidden" id="sharedUserId" name="sharedUserId"/>
<?php echo $form->end('Share');?>
这是 JQuery 函数,用于获取自动建议列表并获取存储在表单隐藏字段中的所选条目的 id。
new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
"http://localhost/FormBuilder/forms/autoComplete",
{
tokens: ',',
afterUpdateElement : getSelectedId
}
);
function getSelectedId(text, li) {
$("#sharedUserId").val(li.id);
}
假设如果我选择多个条目,如何发送这些值? 我可以将数组作为隐藏字段,以便我可以拥有所选元素的数组并将该数组保存为隐藏字段吗?
I am implementing an auto complete box using the Ajax.autocompleter method of the scriptaculous.js framework.
This is the auto complete box and the div where the auto suggested entries are populated.
<?php echo $form->create('Share', array('url' => '/forms/share')); ?>
<label for="shareWith">Share Form with</label>
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" class="autocomplete"></div>
<input type="hidden" id="sharedUserId" name="sharedUserId"/>
<?php echo $form->end('Share');?>
This is the JQuery function to get the auto-suggested list and to get the id of the selected entry which is stored in the hidden field of the form.
new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
"http://localhost/FormBuilder/forms/autoComplete",
{
tokens: ',',
afterUpdateElement : getSelectedId
}
);
function getSelectedId(text, li) {
$("#sharedUserId").val(li.id);
}
Suppose if I select multiple entries,how to send those values?
Can I have an array as a hidden field, so that I can have an array of the selected elements and save that array as a hidden field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需为每个选定的 ID 创建一个新的隐藏输入字段,并确保每个 ID 都有
name="sharedUserId[]"
。 这不遵循 CakePHP 表单元素命名约定,但它将确保sharedUserId
的 POSTed 值是一个数组。Simply create a new hidden input field for every selected ID, and make sure that for each you have
name="sharedUserId[]"
. This doesn't follow the CakePHP form element naming convention, but it will make sure that the POSTed value ofsharedUserId
is an array.用json序列化并在服务器中解析它。 PHP 5.2 可以原生解析 json。
serialize with json and parse it in the server back. PHP 5.2 can parse json natively.
不过与您的问题无关。
http://docs.jquery.com/Plugins/Autocomplete
Not related to your question though..
http://docs.jquery.com/Plugins/Autocomplete