【发布时间】:2012-02-15 10:51:32
【问题描述】:
我想将用户屏幕大小作为我的隐藏字段之一。
在纯 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);
【问题讨论】:
-
带有脚本的第二个变体应该可以工作。
-
对于第二个变体,当我“查看源代码”时,SCRIPT 标签被省略,所以我将内部字符串作为值 [document.write(screen.width + " x " + screen.height );]
-
尝试输入“Full HTML”输入格式,应该可以。
-
我将 [输入格式] 从 [php 代码] 更改为 [完整 HTML],使用了第二个变体,仍然当页面的 [查看源代码] 时,没有 SCRIPT 标签,并且“变成了” ,并且在关闭 INPUT 标记之前有一个空行:... id="edit-submitted-userscreen" value="document.write(screen.width + " x " + screen.height); " /> [感谢您的帮助!]
-
你不能将javascript放在隐藏字段的属性值中。
标签: javascript drupal drupal-webform