如何将数组作为隐藏字段传递?

发布于 2024-08-02 02:15:46 字数 1097 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

差↓一点笑了 2024-08-09 02:15:46

只需为每个选定的 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 of sharedUserId is an array.

筱武穆 2024-08-09 02:15:46

用json序列化并在服务器中解析它。 PHP 5.2 可以原生解析 json。

serialize with json and parse it in the server back. PHP 5.2 can parse json natively.

诗笺 2024-08-09 02:15:46

不过与您的问题无关。

http://docs.jquery.com/Plugins/Autocomplete

  • jQuery 自动完成你

Not related to your question though..

http://docs.jquery.com/Plugins/Autocomplete

  • jQuery Auto complete you
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文