0

伙计们,我有一个创建动态按钮的 ajax,每个按钮的 id 上都有一个计数器。我试图选择每个按钮并向它们添加一个类。

   var html;
                $.ajax({
                          type:'POST',
                          url:'add_subject.php',
                     dataType:'json',
                         data:{'func_numbr':'6'},
                      success:function (data){
                           var counter_sub = 0 ;
                         $.each(data, function(i, item) {
                          if(data[i].subjct_statu == "0"){
                            $('#inactivate['+counter_sub+']').addClass("inactiveButton");
                           }
                          html = "<tr>";

                          html += "<td><lable>Subject: </label><input type='text' name='subject["+counter_sub+"]' rel='"+data[i].subj_id+"' value='"+data[i].subj_name+"'></td>";
                          html += "<td><input type='button' id='activate["+counter_sub+"]' name='active' class='button active  ' value='Apply'  > ";
                          html += "<input type='button' id='inactivate["+counter_sub+"]' name='active' class='button inactive' value='Remove from list'></td>";
                          html += "</tr>";
                 $('#curr-4thhigh-tble').append(html);

                 counter_sub = counter_sub +1;
                });
                      }     
               });

但是我正在做的事情不起作用,我想有什么问题。请帮助 tnx 进阶

4

2 回答 2

0

尝试这样的事情,它应该工作:

$.ajax({
    type: 'POST',
    url: 'add_subject.php',
    dataType: 'json',
    data: {
        'func_numbr': '6'
    },
    success: function(data) {
        var counter_sub = 0,
            classToAdd = "";
        $.each(data, function(i, item) {
            classToAdd = "";
            if (data[i].subjct_statu == "0") {
                classToAdd = "inactiveButton";
                //$('#inactivate[' + counter_sub + ']').addClass("inactiveButton");
            }
            html = "<tr>";

            html += "<td><lable>Subject: </label><input type='text' name='subject[" + counter_sub + "]' rel='" + data[i].subj_id + "' value='" + data[i].subj_name + "'></td>";
            html += "<td><input type='button' id='activate[" + counter_sub + "]' name='active' class='button active  ' value='Apply'  > ";
            html += "<input type='button' id='inactivate[" + counter_sub + "]' name='active' class='button inactive " + classToAdd + "' value='Remove from list'></td>";
            html += "</tr>";
            $('#curr-4thhigh-tble').append(html);
            counter_sub = counter_sub + 1;
        });
    }
});​
于 2012-11-30T05:06:33.317 回答
0

我建议将类添加到它们,因为它们的创建方式与其他评论/答案建议一样。但是..要回答您的问题,这就是您如何选择所有具有包含“myID”的 id 的元素

       $("[id*='+myID+']")

或在一个循环中

        $("[id*='+myID" + counter_sub + "']")
于 2012-11-30T05:10:00.857 回答