doufei8250 2018-09-20 11:15
浏览 70
已采纳

AJAX表单只执行一次(第一次输入)

I looped output of sql select rows via while loop. Each rows have own form. Only first form is working. Others forms are just refreshing page withour any results. On form submit via AJAX update.php function is executed. Its only working at first input field and don't know how to trigger others.

PHP

echo " <div class=\"col-6 flex-vcenter-items fs-1\">
    <form id='form".$row["id"]."' action='' method='POST'>
         <input id='id' name='potnik' value='".$row["id"]."' type='hidden' /> 
         <input id='cas' class=\"form-control cas".$row["id"]." fancy-border\" type=\"text\" name=\"posodobljeni_cas\"/>
         <input class='btn btn-outline-primary' id='submit' type='submit' value='Nastavi uro'>
     <label id=\"info\"></label>
 </form>
</div>";

JS

$( document ).ready(function() {
//Async update
    $(function () {
        let idrow = $("#id").val();
        $('#form'+idrow).on('submit', function (e) {

            let updaterow = "update.php?id=";
            console.log(updaterow + idrow);
            e.preventDefault();

            $.ajax({
                type: 'post',
                url: updaterow + idrow,
                data: $('#form'+idrow).serialize(),
                success: function () {
                    console.log('ura posodobljena');
                    //Koda za vstavljanje
                    console.log("Prejšnji čas",$("#id-ura"+idrow).text());
                    $("#id-ura"+idrow).replaceWith( $(".cas"+idrow).val() );
                    console.log("Novi čas",$(".cas"+idrow).val());
                }
            });
        });
    });
});
  • 写回答

1条回答 默认 最新

  • duandi4238 2018-09-20 11:31
    关注

    Basic HTML rule is each element has unique id, since you are creating forms in loop i will use classes instead of id's

    echo " <div class=\"col-6 flex-vcenter-items fs-1\">
        <form class='form' data-id='".$row["id"]."' action='' method='POST'>
             <input  name='potnik' value='".$row["id"]."' type='hidden' /> 
             <input  class=\"form-control cas".$row["id"]." fancy-border\" type=\"text\" name=\"posodobljeni_cas\"/>
             <input class='btn btn-outline-primary' type='submit' value='Nastavi uro'>
            <label class=\"info\"></label>
        </form>
    </div>";
    

    Now i will change jQuery code according to the class:-

    $( document ).ready(function() {
        $('input[type=submit]').on('click', function (e) {
            e.preventDefault();
            var obj = $(this);
            let idrow = obj.closest("form").data('id'); // i have used data-id in forms
            let updaterow = "update.php?id=";
            $.ajax({
                type: 'post',
                url: updaterow + idrow,
                data: obj.closest("form").serialize(),
                success: function () {
                    //change these code also based on classes
                    console.log('ura posodobljena');
                    console.log("Prejšnji čas",$("#id-ura"+idrow).text());
                    $("#id-ura"+idrow).replaceWith( $(".cas"+idrow).val() );
                    console.log("Novi čas",$(".cas"+idrow).val());
                }
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码