gpt4 book ai didi

javascript - Drupal:如何将 JavaScript 值添加到隐藏的表单组件

转载 作者:行者123 更新时间:2023-11-30 18:32:19 25 4
gpt4 key购买 nike

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

在纯 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:我尝试将节点 [input format] 更改为 [php code]和以下 [form component][value],但它们都不起作用:

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);

最佳答案

就这么简单:

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>

关于javascript - Drupal:如何将 JavaScript 值添加到隐藏的表单组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9291966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com