我想將用戶屏幕大小作爲我的隱藏字段之一。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