jizhicms ajax提交表单提交留言板

曹え 5811 发布于:2022-11-15 13:23:25

html

<form
                    id="contact-form"
                    class="contact-us-form"
                    action="/message/index"
                    method="post"
                  >
           <input type="hidden" name="title" value="{$type['classname']}">
                    <div class="single-input-box">
                      <input type="text" name="user" placeholder="您的称呼" />
                    </div>
                    <div class="single-input-box">
                      <input
                        type="email"
                        name="email"
                        placeholder="您的邮箱"
                      />
                    </div>
                    <div class="single-input-box">
                      <input type="text" name="tel" placeholder="您的手机" />
                    </div>
                    <div class="single-input-box">
                      <textarea
                        name="body"
                        placeholder="留言内容"
                      ></textarea>
                    </div>
          <div class="single-input-box">
                      <input type="text" name="vercode" placeholder="验证码" />
                    </div>
          <div class="single-input-box">
                      <img src="/common/vercode/name/message_vercode?{fun getRandChar(8)}" onclick="this.src=this.src+'?'+Math.random()">
                    </div>
                    <div class="single-input-box">
                      <button type="button" class="btn-primary btn-large subbtn">
                        提交
                      </button>
                    </div>
                  </form>


js

<script>
      $('.subbtn').click(function(){
       
          //以下参数需获取表单填写内容,再提交
            var title= $('input[name="title"]').val();
            var user=$('input[name="user"]').val();
            var email=$('input[name="email"]').val();
            var body=$('input[name="body"]').val();
            var vercode=$('input[name="vercode"]').val();

            let data = {title:title,email:email,user:user,body:body,vercode:vercode,ajax:1};

            console.log(data)

            $.post('/message/index',data,function(res){
               
                if(res.code==0){
                    alert('提交成功!');
                    window.location.href="/";
                }else{
                    alert(res.msg);//弹出错误提示
                }
               
            },'json')


      })
    </script>


觉得有用请点个赞吧!
0 138