douba7784 2019-08-11 10:32
浏览 102
已采纳

仅使用AJAX提交一个表单

I have several forms in my html view (they displayed by php foreach). Form example:

<form method="POST" class="like-form-js">
 <input type="hidden" name="post_id" value="<?= $post['id'] ?>">
 <input type="hidden" name="user_id" value=<?= CoreController::findIndentityId(); ?>">
 <i class="far fa-thumbs-up color-grey c-pointer submit-like"></i>
</form>

I have many such forms in my view. My js:

$('.submit-like').click( function() {
        $.ajax({
            url: 'some-url',
            type: 'post',
            dataType: 'json',
            data: $('.like-form-js').serialize(),
            success: function(data) {
                console.log('success');
            }
        });
    });

I need to submit only one form on which i clicked, but this forms have similar class and they are all submiting. How to solve this?

  • 写回答

1条回答 默认 最新

  • dqlxtv1452 2019-08-11 10:43
    关注

    You have to specify which form to be submitted Try this,

    $('.submit-like').click(function(){
         $(this).parent().submit()
    })
    
    $('like-form-js').submit(function(e){
        e.preventDefault();
        $.ajax({
                url: 'some-url',
                type: 'post',
                dataType: 'json',
                data: $(this).serialize(),
                success: function(data) {
                    console.log('success');
                }
            });
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题