简体   繁体   English

如何选择在jquery中动态变化的id?

[英]how to select id which is dynamically change in jquery?

here is my html:这是我的 html:

 <?php echo ($row['status'] == 1)? ?> <span id='slide_".$row['id']."' name="slid_id" class='label label-danger'>Inactive</span> <?php : ?> <span id='slide_".$row['id']."' name="slid_id" class='label label-success'>Active</span> <?php ; ?>

here is my jquery code:这是我的 jquery 代码:

 $(document).ready(function() { $("span").click(function() { var id = $(":input[name=slide_id]").val(); alert(id); }); });

i didn't get anything , i want to get the我什么都没得到,我想得到

ID & its dynamic generated value ID 及其动态生成的值

of the span element and then perform some action like on click i want to change the status from active to inactive or vise versa. span 元素,然后执行一些操作,例如单击我想将状态从活动更改为非活动,反之亦然。

thanks in advance.提前致谢。

var id = $(":input[name=slide_id]").attr('id'); should do the job.应该做的工作。 .val() only returns the current value of input elements. .val()只返回输入元素的当前值。

Your HTML is missing from the question, but eliminating the colon in your jQuery selector might do the trick in selecting an input field - ie $("input[name=slide_id]").val() .问题中缺少您的 HTML,但消除 jQuery 选择器中的冒号可能会在选择输入字段时$("input[name=slide_id]").val() - 即$("input[name=slide_id]").val()

If you want to get the ID attribute of a span element, this would work:如果您想获取 span 元素的 ID 属性,这将起作用:

$('span_selector').attr('id');

okay finally i got the way to solve this things.好吧,我终于找到了解决这个问题的方法。 m just sharing this if anyone have same issue one day then this might helpful.我只是分享这个,如果有一天有人遇到同样的问题,那么这可能会有所帮助。

html codes: html代码:

<tr class="tr">
    <td class="td">
        <div id="status">
            <?php

            echo ($row['status'] == 1)? 
            "<span id='".$row['id']."' class='label label-danger'>Inactive</span>":
            "<span id='".$row['id']."' class='label label-success'>Active</span>";
            ?>
        </div>
    </td>
</tr>

my jquery code:我的jQuery代码:

$(document).ready(function()
    {
        $("#status > span") .click(function()
        {

            var id = $(this).attr('id');
            var tempelement = $(this);
            var texts = $(this).closest(".tr").find("#texts").val();
            var author = $(this).closest(".tr").find("#author").val();
            var status = $(this).text();
            $.ajax({
                    type: 'POST',
                    url: 'manage_feedback.php',
                    data: {id:id, texts:texts, author:author, status:status},
                    success: function(data)
                    {
                        if (data == "Active")
                        {
                            $(tempelement).removeClass("label label-danger");
                            $(tempelement).addClass("label label-success");
                            $(tempelement).html(data);
                            alert('status changed');
                        }
                        else if (data == "Inactive")
                        {
                            $(tempelement).removeClass("label label-success");
                            $(tempelement).addClass("label label-danger");
                            $(tempelement).html(data);
                            alert('status changed');
                        }
                        else
                        {
                            alert(data);
                        }
                    }
            });
        });

php script php脚本

 //ajax status changer code if (isset($_POST['id']) && isset($_POST['texts']) && isset($_POST['status']) && isset($_POST['author'])) { $id = $_POST['id']; $texts = trim($_POST['texts']); $author = trim($_POST['author']); $status = $_POST['status']; $qry = "SELECT count(id) as count FROM tbl_testimonials WHERE texts = '".$texts."' AND author = '".$author."' AND id != ".$id." "; $sql = mysql_query($qry); $data = mysql_fetch_assoc($sql); if ($data['count'] == 0) { if($status == 'Inactive') { $qry = "UPDATE tbl_testimonials SET status = 0 WHERE id = ".$id." " ; $sql = mysql_query($qry); if($sql == 1) { echo 'Active'; exit; } } elseif ($status == 'Active') { $qry = "UPDATE tbl_testimonials SET status = 1 WHERE id = ".$id." " ; $sql = mysql_query($qry); if($sql == 1) { echo 'Inactive'; exit; } } } else { echo "name already taken"; exit; } }

hope it will help someone.希望它会帮助某人。

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

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