doufei8250
doufei8250
2018-09-20 11:15

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

  • php
  • ajax
  • jquery

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条回答