我想将用户屏幕大小作为我的隐藏字段之一。
在纯 HTML 中,我可以将输入值作为 JavaScript 获取,例如:
document.write(screen.width + " x " + screen.height)
如果我将页面的[输入格式]设置为[完整html]或[php代码],如果我将组件值设置为-
<script>document.write(screen.width + " x " + screen.height);</script>
省略了 SCRIPT 标记,引号变为 " 。似乎 [value] 字段已转换为安全的 HTML 实体。
有没有办法在 Drupal 网络表单中添加一个带有 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);