使用 jquery 将带 href 的 ID 发送到 php 页面

所以,我有一个 CRUD 和一个按钮来修改,应该重定向到另一个带有该人的 id 的页面,我尝试用 jquery 这种方式来做到这一点


    fetchList()

function fetchList() {

    $.ajax({

        url: 'lista.php',

        type: 'GET',

        success: function (response) {

            let task = JSON.parse(response);

            let template = '';

            task.forEach(task => {

                template +=

                    `<tr pacienteId="${task.id_pac}">

                    <td>${task.apellido}  </td>

                    <td>${task.nombre}</td>

                    <td>${task.dni}</td>

                    <td>${task.fecha_nacimiento}</td>

                    <td>${task.fecha_ingreso}</td>

                    <td>${task.obra_social}</td

                    // <td <span class="badge pacActivo">Activo</span> </td>

                    <td>

                        <div class="btn-group btn-group-toggle d-flex justify-content-center">

                        <a href="modificar.php" id="modificar" class="btn btn-sm butMod"></a>

                                    <i class="fas fa-edit"></i>

                            <button class="btn btn-sm butDel darBaja">

                                <i class="fas fa-trash-alt"></i>

                            </button>

                        </div>

                    </td>

                </tr>`

            });

            $("#listadoPacientes").html(template);

        }

    });

}

这是修改


    $(document).on('click', '#modificar', function (e) {

    var href = $(this).attr('href');

    let element = $(this)[0].parentElement.parentElement.parentElement

    let id = $(element).attr('pacienteId')

    // Save information


    // Check if any ID is aviable 

    if (id) {

        // Save the url and perform a page load

         var direccion = href + '&id=' + id;

        window.open(direccion);

        console.log(id)


    } else {

        // Error handling

        alert('algo salio mal')

    }


})

但问题是我收到此错误:


未定义索引:C:\xampp\htdocs\system\modules\Patients\modify.php 中的 id


我在变量内有来自 jquery 的 ID


弑天下
浏览 34回答 1
1回答

慕姐8265434

href解决方案:在 jQuery 中仅使用“&”附加属性。你需要一个“?”&nbsp;而不是“&”。解决方案:您将 GET 参数放在 jQuery HTML 构建过程href标记后面。简而言之,您生成的 URL 如下:&nbsp;modificar.php&id=0&nbsp;正确的是&nbsp;modificar.php**?**id=0例子:if (id) {&nbsp; &nbsp; &nbsp; &nbsp; // Save the url and perform a page load&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var direccion = href + '?id=' + id; // changed the & to an ?&nbsp; &nbsp; &nbsp; &nbsp; window.open(direccion);&nbsp; &nbsp; &nbsp; &nbsp; console.log(id)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // Error handling&nbsp; &nbsp; &nbsp; &nbsp; alert('algo salio mal')&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP