1.把form的action的值改为“javascript:;”
2.把提交按钮的type类型由type="submit" 修改为 type="button" ,给提交按钮添加一个点击事件:onclick="add_ajaxmessage()
代码如下:
<form action="javascript:;" enctype="multipart/form-data" method="post"><input name="action" type="hidden" value="post" />
<input name="diyid" type="hidden" value="1" />
<input name="do" type="hidden" value="2" />
<label>姓名:<input id="name" name="name" type="text" value="" /></label>
<label>电话:<input id="tel" name="tel" type="text" value="" /></label>
<label>手机:<input id="iphone" name="iphone" type="text" value="" /></label>
<label>邮箱:<input id="email" name="email" type="text" value="" /></label>
<table>
<tbody>
<tr>
<td valign="top">留言:</td>
<td><textarea id="liuyan" name="liuyan"></textarea></td>
</tr>
</tbody>
</table>
<input name="dede_fields" type="hidden" value="name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext" />
<input type="hidden" name="dede_fieldshash" value="6291b3115629089aa42c6c33f152f546" />
<input class="submit fr" type="button" value="提 交" onclick="add_ajaxmessage()"/>
</form><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
//定义一个方法,当用户点击提交按钮是触发该方法
function add_ajaxmessage(){
//先获取到表单里每一项的值
var name = $('#name').val();
var tel = $('#tel').val();
var iphone = $('#iphone').val();
var email = $('#email').val();
var liuyan = $('#liuyan').val();
//定义变量接收表单每一项的值和几个dede隐藏的input的值拼接成的字符串
var dataString = 'name='+ name + '&tel=' + tel + '&iphone=' + iphone + '&email=' + email + '&liuyan='+ liuyan +'&action=post'+ '&diyid=1&do=2&dede_fields=name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext'+'&dede_fieldshash=6291b3115629089aa42c6c33f152f546';
//用ajax把表单里的内容提交到dede后台
$.post('/plus/diy.php',dataString,function(data){
alert('提交成功');
});
}
</script>
<input name="diyid" type="hidden" value="1" />
<input name="do" type="hidden" value="2" />
<label>姓名:<input id="name" name="name" type="text" value="" /></label>
<label>电话:<input id="tel" name="tel" type="text" value="" /></label>
<label>手机:<input id="iphone" name="iphone" type="text" value="" /></label>
<label>邮箱:<input id="email" name="email" type="text" value="" /></label>
<table>
<tbody>
<tr>
<td valign="top">留言:</td>
<td><textarea id="liuyan" name="liuyan"></textarea></td>
</tr>
</tbody>
</table>
<input name="dede_fields" type="hidden" value="name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext" />
<input type="hidden" name="dede_fieldshash" value="6291b3115629089aa42c6c33f152f546" />
<input class="submit fr" type="button" value="提 交" onclick="add_ajaxmessage()"/>
</form><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
//定义一个方法,当用户点击提交按钮是触发该方法
function add_ajaxmessage(){
//先获取到表单里每一项的值
var name = $('#name').val();
var tel = $('#tel').val();
var iphone = $('#iphone').val();
var email = $('#email').val();
var liuyan = $('#liuyan').val();
//定义变量接收表单每一项的值和几个dede隐藏的input的值拼接成的字符串
var dataString = 'name='+ name + '&tel=' + tel + '&iphone=' + iphone + '&email=' + email + '&liuyan='+ liuyan +'&action=post'+ '&diyid=1&do=2&dede_fields=name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext'+'&dede_fieldshash=6291b3115629089aa42c6c33f152f546';
//用ajax把表单里的内容提交到dede后台
$.post('/plus/diy.php',dataString,function(data){
alert('提交成功');
});
}
</script>
还有一种更简单的方法,不用获取每个input的值,也不用拼接字符串,直接用jquery表单序列化的方式提交,$('form').serialize()可以将整个表单的值序列化。
<form action="javascript:;" enctype="multipart/form-data" method="post"><input name="action" type="hidden" value="post" />
<input name="diyid" type="hidden" value="1" />
<input name="do" type="hidden" value="2" />
<label>姓名:<input id="name" name="name" type="text" value="" /></label>
<label>电话:<input id="tel" name="tel" type="text" value="" /></label>
<label>手机:<input id="iphone" name="iphone" type="text" value="" /></label>
<label>邮箱:<input id="email" name="email" type="text" value="" /></label>
<table>
<tbody>
<tr>
<td valign="top">留言:</td>
<td><textarea id="liuyan" name="liuyan"></textarea></td>
</tr>
</tbody>
</table>
<input name="dede_fields" type="hidden" value="name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext" />
<input type="hidden" name="dede_fieldshash" value="6291b3115629089aa42c6c33f152f546" />
<input class="submit fr" type="button" value="提 交" onclick="add_ajaxmessage()"/>
</form><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
//定义一个方法,当用户点击提交按钮是触发该方法
function add_ajaxmessage(){
//用ajax把表单里的内容提交到dede后台
$.post('/plus/diy.php',$('form').serialize(),function(data){
alert('提交成功');
});
}
</script>
<input name="diyid" type="hidden" value="1" />
<input name="do" type="hidden" value="2" />
<label>姓名:<input id="name" name="name" type="text" value="" /></label>
<label>电话:<input id="tel" name="tel" type="text" value="" /></label>
<label>手机:<input id="iphone" name="iphone" type="text" value="" /></label>
<label>邮箱:<input id="email" name="email" type="text" value="" /></label>
<table>
<tbody>
<tr>
<td valign="top">留言:</td>
<td><textarea id="liuyan" name="liuyan"></textarea></td>
</tr>
</tbody>
</table>
<input name="dede_fields" type="hidden" value="name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext" />
<input type="hidden" name="dede_fieldshash" value="6291b3115629089aa42c6c33f152f546" />
<input class="submit fr" type="button" value="提 交" onclick="add_ajaxmessage()"/>
</form><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
//定义一个方法,当用户点击提交按钮是触发该方法
function add_ajaxmessage(){
//用ajax把表单里的内容提交到dede后台
$.post('/plus/diy.php',$('form').serialize(),function(data){
alert('提交成功');
});
}
</script>