简体   繁体   English

PHP 循环提交按钮在 AJAX 中不起作用

[英]PHP while loop submit button doesn't work in AJAX

I have a PHP while loop which gets test_id for quizzes from mysql db and creates button for each unique test_id.我有一个 PHP while 循环,它从 mysql db 获取测验的 test_id,并为每个唯一的 test_id 创建按钮。 So all quizzes show up on a page as follows:所以所有的测验都显示在一个页面上,如下所示:

cant upload an image so it's on imgur.com list of quizzes无法上传图片,所以它在 imgur.com测验列表中

My method for displaying the quizzes and to capture button click is as follows: 1. txtHint displays the quiz list 2. take-quiz is button id 3. test_id is sent using a hidden input type which has unique test_id which changes with each while loop.我显示测验和捕获按钮点击的方法如下: 1. txtHint 显示测验列表 2. 参加测验是按钮 ID 3. test_id 使用隐藏输入类型发送,它具有唯一的 test_id,随着每个 while 循环而变化.

$.ajax({

                    type: "POST",crossDomain: true, cache: false,
                    url: url,
                    data: loginString,
                    success: function(data1){
                            localStorage.loginstatus = "true";
                     $("#txtHint").html(data1);

                    $("#take-quiz").click(function(){


                            // update profile.php,
                            var test_id= $.trim($("#test_id").val());


                            localStorage.setItem("test_id", test_id);
                            window.location.href = "take-quiz.html";


               });

                }
                });

Here is some php code within while loop which shows the button and hidden test_id这是 while 循环中的一些 php 代码,它显示了按钮和隐藏的 test_id

$createTable .= '<input type="hidden" value="';
    $createTable .= $row['id'];
    $createTable .= '"  id="test_id" />';
    $createTable .= '<input type="submit" value="Take Quiz " name="take-quiz" id="take-quiz" class="btn btn-primary"/>';

This list is displayed using AJAX html element.此列表使用 AJAX html 元素显示。 when I try to click the buttons only the first result button works, others don't do anything.当我尝试单击按钮时,只有第一个结果按钮有效,其他按钮什么都不做。 If i try to bind the click event to the document each button works but I get the same test_id for each button.如果我尝试将点击事件绑定到文档,每个按钮都可以工作,但我为每个按钮获得相同的 test_id。

$(document).on('click', '#take-quiz', function(e) {
    alert(...);
});

I looked at another question on SO which is a duplicate: Cant click on button with is loaded with ajax but I cannot get it to resolve my errors.我查看了另一个关于 SO 的重复问题: Cant click on button with is loaded with ajax但我无法解决我的错误。

Can someone please shed some light on what I am doing wrong?有人可以阐明我做错了什么吗?

Many thanks.非常感谢。

First, replace your duplicate IDS with a class. IDS must be unique per page and not duplicated.首先,将重复的 IDS 替换为 class。每个页面的 IDS 必须是唯一的,不能重复。 Second, you will want to change your click handler to take into consideration the class as well as remove it from the ajax.其次,您需要更改点击处理程序以考虑 class 并将其从 ajax 中删除。

$createTable .= '<input type="hidden" value="';
    $createTable .= $row['id'];
    $createTable .= '"  class="test_id" />';
    $createTable .= '<input type="submit" value="Take Quiz " name="take-quiz" class="btn btn-primary take-quiz"/>';



  $.ajax({
    type: "POST",crossDomain: true, cache: false,
    url: url,
    data: loginString,
    success: function(data1){
        localStorage.loginstatus = "true";
        $("#txtHint").html(data1);
    }
});

$(document).on("click",".take-quiz",function(){
    var test_id= $.trim($(this).prev().val());
    localStorage.setItem("test_id", test_id);
    window.location.href = "take-quiz.html";
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM