需 求* z# n7 ~6 [9 P3 h
在Drupal中自己定义一个表单,然后用ajax提交。
) G: q- }* G6 V7 y6 n, e# \' \, o7 }
解决方案 用html写一个form, 然后在form外面设置一个Button, 在button的onclick事件中调用jQuery.post()函数,将表单进行提交。
t9 W9 G) {( \( v; f3 l有下面两点需要注意: - button一定要放在表单外面,否则就会刷新页面了
- 每个input的控件必须有name, 如果没有name是无法获得数据的
- 在用jQuery.post()进行提交时,需要将form序列化,使用jQuery的Helper的.serialize()即可
: M, s/ w* }5 v; v) t
示例代码
/ q0 ]; `/ L% G# A z7 O6 T0 n1 I <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>
1 N4 ? i% c+ X& B- }; N: s- [% F
这样在提交后,在/post/form路径中的$_POST会获得一个提交数组,如下面所示:
; _# }, K& f4 ~& F9 Zarray(2) { ["txt_name"]=> string(1) "Blade" ["txt_age"]=> string(1) "100" } 8 m: b( Q6 A. ~6 k0 m2 u
9 s& @; p$ o/ } |