AJAX提交表单 H5input的部分属性(required)仍然有效

本文介绍了一个使用HTML和JavaScript实现的表单提交案例,通过AJAX进行异步提交,避免了页面刷新,提供了更好的用户体验。代码中包含了表单元素、jQuery库的引入以及提交函数的定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML代码

<form onsubmit="comment()" id="form_comment" action="javascript:void(0)">
	<input type="text" name="article_id" />
	<li class="list-group-item">
		<textarea name="content" class="form-control" rows="10" style="resize:none;" required></textarea>
		<button class="btn btn-default" type="submit">提交</button>
	</li>
</form>

JS代码

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
    function comment() {
        $.ajax({
            url:"action/commentAction.php",	//提交地址
            type:"post",	//提交方式
            data:$('#form_comment').serialize(),	//表单内所有信息
            success: function (result) {
            	var obj=JSON.parse(result)
                if(obj.type=="请登录")
                {
                    window.location.href='login.php';
                }
                else if(obj.type=="发表成功")
                {
                    location.reload()
                }
                else
                {
                    alert("异常")
                }
            },
            error : function(result) {
                console.log(result)
            }
        });
    };
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值