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

发布于 2025-01-05 07:23:20 字数 1966 浏览 2 评论 0原文

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

在纯 HTML 中,我可以获取 JavaScript 形式的输入值,例如:

document.write(screen.width + " x " + screen.height)

如果我将页面的[输入格式]设置为[完整 html] 或 [php 代码],如果我将组件值设置为 - 则

<script>document.write(screen.width + " x " + screen.height);</script>

省略 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(&#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);

I want to get the user screen size as one of my hidden fields.

In pure HTML, I can get the input value as JavaScript, e.g.:

document.write(screen.width + " x " + screen.height)

If I set[input format] of the page to [full html] or [php code], if I set the component value to -

<script>document.write(screen.width + " x " + screen.height);</script>

the SCRIPT tags are omitted, and the quote signs becomes " . It seems that the [value] field is transformed to safe HTML entities.

Is there a way to add a hidden form component with JavaScript value, in Drupal webforms?

EDIT: Solved:

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]

Edit:
I can add some PHP values as

%server[HTTP_USER_AGENT]

How do I add JavaScript values?

Edit 2:
I tried changing the node [input format] to [php code]
and the following [form component][value], but all of them did not work:

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 tags are replaced with NewLineChar ???]

=> so when I fill in the form, I receive email with the original string:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-12 07:23:20

它很简单:

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

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

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

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

已编辑

尝试在启用完整 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>

It's as simple as:

1) Define a hidden field in your form.

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

2) Apply value with jQuery to your field.

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

EDITED

Try to provide this value to your textarea with Full HTML mode enabled.

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

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

SECOND EDIT

<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="" />
<script>document.getElementById('edit-submitted-userscreen').value = screen.width + 'x' + screen.height;</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文