1

我想将用户屏幕大小作为我的隐藏字段之一。

在纯 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(&#039;#edit-submitted-userscreen&#039;).val(screen.width + &#039;x&#039; + screen.height);


"  />

[SCRIPT 标签被替换为 NewLineChar ???]

=> 所以当我填写表格时,我会收到带有原始字符串的电子邮件:

userScreen: jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
4

1 回答 1

2

它很简单:

1)在表单中定义一个隐藏字段。

$form['resolution'] = array(
  '#type' => 'hidden'
);

2) 使用 jQuery 将值应用于您的字段。

$('#edit-resolution').val(screen.width + " x " + screen.height);

已编辑

尝试在Full HTML启用模式的情况下将此值提供给您的文本区域。

<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="" />

<script>
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height);
</script>

第二次编辑

<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="" />
<script>document.getElementById('edit-submitted-userscreen').value = screen.width + 'x' + screen.height;</script>
于 2012-02-15T11:07:54.443 回答