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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致