2012-02-15 55 views
1

我想将用户屏幕大小作为我的隐藏字段之一。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(&#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); 
+0

用脚本的第二个变体应该工作。 – 2012-02-17 23:14:40

+0

与第二个变体,当我“查看源代码”的SCRIPT标记被省略,所以我得到的内部字符串作为值[document.write(screen.width +“x”+ screen.height);] – Atara 2012-02-19 12:41:33

+0

尝试把“完整的HTML”输入格式,这应该工作。 – 2012-02-19 13:07:52

回答

2

这是一个简单如:

1)在窗体中定义一个隐藏字段。

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

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

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

EDITED

尽量提供这个价值,您的textarea启用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> 
+0

我使用GUI添加表单组件,应该是[Value]字段[$ val(screen.width +“x”+ screen.height] – Atara 2012-02-16 16:40:33

+0

对不起,没有明白你的意思? – 2012-02-16 17:26:25

+0

到现在为止,我使用[form component]创建了窗体[edit] ]按钮,我应该在哪里写你提供给我的代码? – Atara 2012-02-16 17:44:47

相关问题