我想将用户屏幕大小作为我的隐藏字段之一。Drupal:如何将JavaScript值添加到隐藏窗体组件
在纯HTML,我可以输入值,如JavaScript,如:
document.write(screen.width + " x " + screen.height)
如果我设置页面[输入格式]到[HTML全文]或[PHP代码],如果我设置该组件的价值 -
<script>document.write(screen.width + " x " + screen.height);</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);
用脚本的第二个变体应该工作。 – 2012-02-17 23:14:40
与第二个变体,当我“查看源代码”的SCRIPT标记被省略,所以我得到的内部字符串作为值[document.write(screen.width +“x”+ screen.height);] – Atara 2012-02-19 12:41:33
尝试把“完整的HTML”输入格式,这应该工作。 – 2012-02-19 13:07:52