Drupal:如何将 JavaScript 值添加到隐藏表单组件
我想将用户屏幕尺寸作为我的隐藏字段之一。
在纯 HTML 中,我可以获取 JavaScript 形式的输入值,例如:
document.write(screen.width + " x " + screen.height)
如果我将页面的[输入格式]设置为[完整 html] 或 [php 代码],如果我将组件值设置为 - 则
<script>document.write(screen.width + " x " + screen.height);</script>
省略 SCRIPT 标签,并且引号变成 " 。似乎 [value] 字段已转换为安全的 HTML 实体。
有没有办法在 Drupal webforms 中添加带有 JavaScript 值的隐藏表单组件?
编辑:已解决:
1 - Copy the original
...sites/all/modules/webform/webform-form.tpl.php
to
...sites/all/themes/myTheme/webform-form-myFormNodeId.tpl.php
2 - Add the following lines to webform-form-myFormNodeId.tpl.php
echo "<script>";
echo "document.getElementById('edit-submitted-userscreen').value = screen.width + 'x' + screen.height;";
echo "</script>";
3 - admin/settings/performance : [Clear cached data]
编辑: 我可以添加一些 PHP 值,如
%server[HTTP_USER_AGENT]
如何添加 JavaScript 值?
编辑2: 我尝试将节点[输入格式]更改为[php代码] 以及下面的[表单组件][值],但它们都不起作用:
1 - drupal_add_js(screen.width + " x " + screen.height);
2 - <script>document.write(screen.width + " x " + screen.height);</script>
3 - <?php echo "<script language=\"javascript\">" . "document.write (screen.width + ' x ' + screen.height);" . "</script>"; ?>
4 - full html +
<script>
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
</script>
=> in View Page Source, I see:
<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
" />
[SCRIPT标签被替换为NewLineChar ???]
=>因此,当我填写表格时,我会收到包含原始字符串的电子邮件:
userScreen: jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
I want to get the user screen size as one of my hidden fields.
In pure HTML, I can get the input value as JavaScript, e.g.:
document.write(screen.width + " x " + screen.height)
If I set[input format] of the page to [full html] or [php code], if I set the component value to -
<script>document.write(screen.width + " x " + screen.height);</script>
the SCRIPT tags are omitted, and the quote signs becomes " . It seems that the [value] field is transformed to safe HTML entities.
Is there a way to add a hidden form component with JavaScript value, in Drupal webforms?
EDIT: Solved:
1 - Copy the original
...sites/all/modules/webform/webform-form.tpl.php
to
...sites/all/themes/myTheme/webform-form-myFormNodeId.tpl.php
2 - Add the following lines to webform-form-myFormNodeId.tpl.php
echo "<script>";
echo "document.getElementById('edit-submitted-userscreen').value = screen.width + 'x' + screen.height;";
echo "</script>";
3 - admin/settings/performance : [Clear cached data]
Edit:
I can add some PHP values as
%server[HTTP_USER_AGENT]
How do I add JavaScript values?
Edit 2:
I tried changing the node [input format] to [php code]
and the following [form component][value], but all of them did not work:
1 - drupal_add_js(screen.width + " x " + screen.height);
2 - <script>document.write(screen.width + " x " + screen.height);</script>
3 - <?php echo "<script language=\"javascript\">" . "document.write (screen.width + ' x ' + screen.height);" . "</script>"; ?>
4 - full html +
<script>
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
</script>
=> in View Page Source, I see:
<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
" />
[SCRIPT tags are replaced with NewLineChar ???]
=> so when I fill in the form, I receive email with the original string:
userScreen: jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它很简单:
1) 在表单中定义一个隐藏字段。
2) 使用 jQuery 将值应用到您的字段。
已编辑
尝试在启用
完整 HTML
模式的情况下将此值提供给您的文本区域。第二次编辑
It's as simple as:
1) Define a hidden field in your form.
2) Apply value with jQuery to your field.
EDITED
Try to provide this value to your textarea with
Full HTML
mode enabled.SECOND EDIT