2012-02-15 37 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

相關問題