需 求
$ P4 M V# ]9 L; [在Drupal中自己定义一个表单,然后用ajax提交。 w+ e: L3 V# J
解决方案 用html写一个form, 然后在form外面设置一个Button, 在button的onclick事件中调用jQuery.post()函数,将表单进行提交。* L6 O3 N% e, U2 o! ^
有下面两点需要注意: - button一定要放在表单外面,否则就会刷新页面了
- 每个input的控件必须有name, 如果没有name是无法获得数据的
- 在用jQuery.post()进行提交时,需要将form序列化,使用jQuery的Helper的.serialize()即可
v7 o0 w* w. O) P& p
示例代码1 I7 y6 Z1 y4 F, d* S
<form id="form_charge"><label for="txt_name"> Name:</label> <input type="text" name="txt_name" id="txt_name"></input><label for="txt_age"> Age:</label> <input type="text" name="txt_age" id="txt_age"></input></form><button onclick="$.post('/post/form', $('#form_charge').serialize(), function(data){$('#box_charge_result').html(data);});">Go!</button>
" u" K% e3 b6 L; x+ M
+ ~+ D3 F4 P ^3 c/ G, u2 d3 R# Y这样在提交后,在/post/form路径中的$_POST会获得一个提交数组,如下面所示:
8 n0 } ?: i# t6 e; |8 aarray(2) { ["txt_name"]=> string(1) "Blade" ["txt_age"]=> string(1) "100" } ; M: s% s* ^/ e* l3 R/ p" n, j
2 m# E I/ {) v; V
|