|
需 求% u$ i* H! C2 Z2 E# f1 w' L
在Drupal中自己定义一个表单,然后用ajax提交。 + P: H7 |7 d! H- u' ?0 x
解决方案 用html写一个form, 然后在form外面设置一个Button, 在button的onclick事件中调用jQuery.post()函数,将表单进行提交。
# D3 D# S* y# q- ]* |% |有下面两点需要注意: - button一定要放在表单外面,否则就会刷新页面了
- 每个input的控件必须有name, 如果没有name是无法获得数据的
- 在用jQuery.post()进行提交时,需要将form序列化,使用jQuery的Helper的.serialize()即可
5 k8 [/ `1 \7 T% u" R
示例代码
0 H: D. A% ~9 ]/ | <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>
3 H% }, o# x# u6 ?, `( Q
( f! Y3 ~! l+ k0 f这样在提交后,在/post/form路径中的$_POST会获得一个提交数组,如下面所示:
. d% ~; R) i5 T% I/ a6 Larray(2) { ["txt_name"]=> string(1) "Blade" ["txt_age"]=> string(1) "100" }
, N" ?7 ?! b1 B, ], A, K& e7 Z
|