使用Jquery从值中获取id号

时间:2015-11-13 08:11:13

标签: javascript jquery

我有以下代码:

 var currentuuid = $('#UUIDTOSEND').val();
        html1 = '';
        $.ajax({
            type: "GET",
            url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=stitrtmnt",
            dataType: "JSON",
            success: function (data) {
                client_history_list = $('#clinical_history_tbody').empty();

                for (i = 0; i < data.length; i++) {

                    html1 += '<tr>\n\
                        <td class = "form-group">\n\
            \n\
                        <input type = "text"   class = "form-control facility_name col-md-6 col-sm-6 col-xs-12 "  readonly="" name="facility_name[]" id = "facility_name" value="' + data[i].facility_name + '" placeholder = "Facility Name">\n\
            </td>\n\
            <td class = "form-group">\n\
            \n\
            <input type = "text"  class = "form-control activity_timestamp col-md-6 col-sm-6 col-xs-12 "  readonly="" name="activity_timestamp[]" id = "activity_timestamp" value="' + data[i].activity_timestamp + '" placeholder = "Visit Date">\n\
            </td>\n\\n\
 <td class = "form-group">\n\<input type = "text"  class = "form-control clinical_visit_id' + data[i].id + ' "  readonly="" name="clinical_visit_id[]" id = "clinical_visit_id" value="' + data[i].id + '" placeholder = "Visit ID">\n\
            \n\
            <button id="view_more' + data[i].id + '"  class="form-control view_more' + data[i].id + '" >Select/View More</button>\n\
            </td>\n\
            </tr>';
                    $("#clinical_history_tbody").on("click", ".view_more" + data[i].id, function () {

                        var clinic_visit_ids = $("#" + this.id.replace("view_more", "clinical_visit_id")).val();
                        alert(clinic_visit_ids);

                    });
                }
                $('#clinical_history_tbody').empty();
                $('#clinical_history_tbody').append(html1);
            }
        });

使用数据库中的数据生成自动表。 我想在单击“选择/查看更多”按钮时从按钮获取id的值,该按钮在脚本上运行以下$("#clinical_history_tbody").on("click", ".view_more" + data[i].id, function () {函数。如何获取按钮ID的值并替换它以便我可以在临床访问中使用它? (当我提醒我得到一个未定义的)。

4 个答案:

答案 0 :(得分:0)

您可以使用此脚本获取ID

$("#clinical_history_tbody").on("click", "[class^=view_more]", function () {
    alert($(this).attr("id"));
});

上面的脚本会将click事件绑定到其类名称以view_more开头的所有元素。然后它会提醒当前点击的元素id。

答案 1 :(得分:0)

&{34; clinical_visit&#34; input永远不会在id的末尾分配数值。

<input type = "text"  class = "form-control clinical_visit_id' + data[i].id + ' "  readonly="" name="clinical_visit_id[]" id = "clinical_visit_id" value="' + data[i].id + '" placeholder = "Visit ID">

而是连接到class

当您想要进行&#34;临床访问时,这是错误的。字段,因为您正在寻找值为id&#34; clinical_visit_id [number]&#34;。 它不存在,所以你最终得到一个空值:

var clinic_visit_ids = $("#" + this.id.replace("view_more", "clinical_visit_id")).val();

试试这个:

<input type = "text"  class = "form-control clinical_visit_id' + data[i].id + ' "  readonly="" name="clinical_visit_id[]" id = "clinical_visit_id'+data[i].id+'" value="' + data[i].id + '" placeholder = "Visit ID">

寻找&#34; clinical_visit&#34;由class而不是id输入(请记住,您仍然需要清理HTML,因为两个元素永远不应该具有相同的id

看起来像这样:

$("#clinical_history_tbody").on("click", ".view_more" + data[i].id, function () {
    var clinic_visit_ids = $("." + this.id.replace("view_more", "clinical_visit_id")).val();
    alert(clinic_visit_ids);
 });

答案 2 :(得分:0)

这是您更改按钮ID的方式:

$("#clinical_history_tbody").on("click",  function () {

    var clinic_visit_ids = $(this).attr('id', "clinical_visit_id");

});

答案 3 :(得分:-1)

将事件附加到onClick上的该按钮

 onClick=giveMeID(data[i].id) 

在Html中,然后在JS中定义

function giveMeID(id){
//Do whatever you want
}

此处的示例JSBIN DEMO

<html>

<body>
    <button id="exa" onclick="genExamp()">Generate Example</button>
    <div id="ex"></div>
    <script>
        function genExamp(){
             var divEx = document.getElementById("ex");
                for (var i = 0; i < 20; i++) {
                        var childBtn='<button id="view_more'+i+'" onclick="giveMeID('+i+');">View/More'+i+'</button>';
                divEx.innerHTML+=childBtn;

                     }
        }
        //On Click:Gets ID and Replace it 
        function giveMeID(obj){
            var origID="view_more"+obj;
                var btn=document.getElementById('view_more'+obj);
            btn.id="view_more"+obj*10;
            var new_id=btn.id;
            alert("Original ID:"+origID+"\n"+"New ID:"+new_id);
        }
    </script>
</body>
</html>