国外设计欣赏网站 - DOOOOR.com
标题: Drupal:自己定义一个表单,然后用ajax提交---jQuery.post的用法 [打印本页]
作者: howfar 时间: 1-29-2012 16:26
标题: Drupal:自己定义一个表单,然后用ajax提交---jQuery.post的用法
需 求' b& B8 E! C7 I L
在Drupal中自己定义一个表单,然后用ajax提交。
' m% c% I' Y' s# i% Y3 X
解决方案
用html写一个form, 然后在form外面设置一个Button, 在button的onclick事件中调用jQuery.post()函数,将表单进行提交。' L( |6 S1 U" C! F
有下面两点需要注意:
- button一定要放在表单外面,否则就会刷新页面了
- 每个input的控件必须有name, 如果没有name是无法获得数据的
- 在用jQuery.post()进行提交时,需要将form序列化,使用jQuery的Helper的.serialize()即可" @8 E& h7 k, y; W1 j
示例代码 m9 C, D- n2 M0 ?3 ]
<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>+ j/ B; R) V# \8 M/ w1 o2 M
; O* s3 F! f ]这样在提交后,在/post/form路径中的$_POST会获得一个提交数组,如下面所示: a5 f* B0 }7 o4 a5 j" d8 F0 Y4 \
array(2) { ["txt_name"]=> string(1) "Blade" ["txt_age"]=> string(1) "100" }
$ n+ c8 t( S4 i4 `6 w/ ^) O
/ j8 I0 b- H7 ]) i2 V) J2 |) x/ b0 D
欢迎光临 国外设计欣赏网站 - DOOOOR.com (https://www.doooor.com/) |
Powered by Discuz! X3.4 |