简体   繁体   English

使用jQuery从PHP动态创建的复选框中获取值

[英]Getting values from php dynamically created checkboxes with jquery

This is not a duplicate of a question suggested as duplicate as this question has nothing to do with event binding 这不是一个建议重复的问题的重复,因为该问题与事件绑定无关

Hi, Im trying to get the value of a check box to send in a ajax request to a php page, the checkboxes are dynamically created using php. 嗨,我试图获取复选框的值以将ajax请求发送到php页面,这些复选框是使用php动态创建的。 So far my code allows me to show an instruction to the user telling the user to choose an admin to delete from the list dynamically created. 到目前为止,我的代码允许我向用户显示一条指令,告诉用户选择要从动态创建的列表中删除的管理员。 Then when a checkbox is checked the instrcution hides and the delete admin button is shown. 然后,当选中一个复选框时,仪器将隐藏,并显示删除管理员按钮。 Next on clicking the delete button I'm trying to confirm the user wants to delete the admin chosen and click to confirm or cancel. 接下来,单击删除按钮,我试图确认用户要删除所选的管理员,然后单击以确认或取消。 The real problem I'having is getting the value of the chosen checked checkbox to pass to the php processing page, so far I have managed to get the value of the first checkbox to pass no matter which checkbox is checked 我真正的问题是获取选中的复选框的值传递给php处理页面,到目前为止,无论选中哪个复选框,我都设法获取了第一个复选框的值

Jquery jQuery的

<script type="text/javascript">

$(document).ready(function() {

    var checkboxes = $("input[type='checkbox']");

checkboxes.click(function() {
    $('.delete_admin_but').show();
    $('#adminDeleteNotice').hide();
    var deleteAdminName=$(this).attr('id');
});

    $(".delete_admin_but").click(function() {



//  if(confirm("Are you sure you want to delete the Admin")) {

    $("#deleteAdminError").html('<img src="image/ajax-loader.gif" width="16" height="16" alt=""/>');




$.post("includes/delete_admin.inc.php",{deleteAdminname:deleteAdminName},function(json)   {
    if(json.result === "success") {
        $("#deleteAdminError").html(json.message);
//  $('.add_intern').get(0).reset();
    }else{
        $("#deleteAdminError").html(json.message);
    }
});



});//submit click
});//doc ready
</script>

html form html表格

<div id="deleteAdmin" style="display:none">

<form id="adminDelete">

          <div class="delete_admin_list">

            <?php while($row = mysqli_fetch_array($result1)) { ?>

              <input type="checkbox" id="<?php echo $row['name']; ?>" value="<?php echo $row['id']; ?>" name="delete[]" class="checkboxAdmin" />

              <div id="admin_db_detail"><?php echo $row['name']; ?> - <?php echo $row['email']; ?></div>

              <?php } ?>

      </div>        


<div id="adminDeleteNotice" style="display:block">Please Choose an Admin to Delete</div>

<input name="delete_admin_but" id="delete_admin_but" type="button" class="delete_admin_but" value="Delete Admin" style="display:none"/>

</form>

<div id="deleteAdminError"></div>

</div>

If anyone could help me figure this out I would be greatful 如果有人可以帮助我解决这个问题,我将非常感激

I have figured this out now, so posting to show how and to make a point that this had no relevance to the suggested duplication of question as was suggested. 我现在已经弄清楚了,因此发帖说明如何并指出这一点与所建议的重复问题无关。 Maybe those who suggest these duplicates should read the question properly first, and then the question they are suggesting as being duplicated to really see if there is any relevance, However just in case I'm wrong I will be very happy to withdraw this comment should someone show me the relevance of the suggested question with the question I posted and providing a solution for. 也许那些建议这些重复的人应该先正确阅读该问题,然后再建议他们重复这些问题以真正查看是否存在相关性。但是,以防万一我错了,我很乐意撤回该评论,有人向我展示了建议的问题与我发布的问题的相关性,并提供了解决方案。

My solution 我的解决方案

jquery changed to the following jQuery更改为以下

<script type="text/javascript">

$(document).ready(function() {

    var checkboxes = $("input[type='checkbox']");

checkboxes.click(function() {
    $('.delete_admin_but').show();
    $('#adminDeleteNotice').hide();

});

    $(".delete_admin_but").click(function() {

$.post("includes/delete_admin.inc.php",{ checked_box : $('input:checkbox:checked').val()},function(json)   {
    if(json.result === "success") {
        $("#deleteAdminError").html(json.message);
//  $('.add_intern').get(0).reset();
    }else{
        $("#deleteAdminError").html(json.message);
    }
});
        });//submit click
});//doc ready
</script>

The change I have made from the original script is with the $.post line 我对原始脚本所做的更改是使用$ .post行

Original code 原始码

$.post("includes/delete_admin.inc.php",{deleteAdminname:deleteAdminName},function(json)   {

New code that gets the checkbox value from the checkbox that is chosen 从所选复选框获取复选框值的新代码

$.post("includes/delete_admin.inc.php",{ checked_box : $('input:checkbox:checked').val()},function(json)   {

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

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