从$ .post结果创建的复选框中获取价值

肖恩·霍尔

我有一组从$ .post返回结果创建的复选框。每次从下拉列表中选择一个不同的人时,就会创建这些复选框,从而为每个人显示不同的人。

$.post("api.php/atherapists", {clientID: clientID}).done(function(data){

  var data = JSON.parse(data);

  var trHTML = '';

  $.each(data, function(i, item){
      therapistID = item.therapist_id;
      therapistName = item.therapist_name;

      trHTML += '<tr><td><input type="checkbox" name="achkbox" id="achkbox" value="' + therapistID + '"></td><td>' + therapistName + '</td></tr>';
  });
  $('#tableAvailable').empty();
  $('#tableAvailable').append(trHTML);

});

我的问题是关于单击时从复选框获取值。我计划在单击复选框之一并将其值发送到php进行处理时,根据$ .post触发mysqli查询的功能。每当我尝试提醒选中的复选框的值时,都不会发生任何事情。我已经尝试过这些

$("input[name='achkbox']").change(function(){
    alert($(this).attr('value'));       
});

$('#achkbox').click(function(){
    alert($(this).attr('value'));        
});

$('#achkbox').change(function(){
    var id = $(this).val();
    if(id != null){
        alert(id);
    }
});

似乎什么都没有返回/显示我需要发送到php来处理该人的值。我可以弄清楚如何将数据即时发送到php,但我只是无法弄清楚我在获取值方面做错了什么。

编辑-2/23/2017我已经落实了费尔明的建议,这很漂亮,但这是我的难题

$.post("api.php/atherapists", {clientID: clientID}).done(function(data){

    var data = JSON.parse(data);

    var table = $('#tableAvailable');
    var trHTML = '';
    $('#tableAvailable').empty();

    $.each(data, function(i, item){
        therapistID = item.therapist_id;
        therapistName = item.therapist_name;

        var tr = $('<tr></tr>');
        var td1 = $('<td></td>');
        var td2 = $('<td></td>');
        var checkbox = $('<input type="checkbox" name="achkbox" id="achkbox" value="' + therapistID + '" >').click(function(){

           var theraID = $(this).val();

           //fire ajax call to assign selected therapist to client
           $.post("api.php/assignsave", {clientID: clientID, therapist: theraID}).done(function(data){

           //after assignment refresh the table by running $.post("api.php/atherapists) again
           //maybe making this whole thing a function then recalling it after post

           });


        });

     tr.append(td1.append(checkbox)).append(td2.append(therapistName));
     table.append(tr);
   });


});

编辑2/23/2017 9:55 am

通过使用Fermin解决方案和某些功能的混合物使它正常工作。这就是我所做的

function getATherapists(clientID){
    $.post("api.php/atherapists", {clientID: clientID}).done(function(data){

        var data = JSON.parse(data);

        var table = $('#tableAvailable');
        var trHTML = '';
        $('#tableAvailable').empty();

        $.each(data, function(i, item){
            therapistID = item.therapist_id;
            therapistName = item.therapist_name;

            var tr = $('<tr></tr>');
            var td1 = $('<td></td>');
            var td2 = $('<td></td>');
            var checkbox = $('<input type="checkbox" name="achkbox" id="achkbox" value="' + therapistID + '" >').click(function(){

            var theraID = $(this).val();

            //assign therapist to client
            $.post("api.php/assignsave", {clientID: clientID, therapist: theraID}).done(function(data){

                //call getATherapists function
                getATherapists(clientID);

                //create a new function to get current therapists since getCTherapists is further down the list  
                function getCurrentTherapists(clientID){
                    $.post("api.php/ctherapists", {clientID: clientID}).done(function(data){

                        var data = JSON.parse(data);

                        var table = $('#tableCurrent');
                        var trHTML = '';
                        $('#tableCurrent').empty();

                        $.each(data, function(i, item){
                            therapistID = item.therapist_id;
                            therapistName = item.therapist_name;

                            var tr = $('<tr></tr>');
                            var td1 = $('<td></td>');
                            var td2 = $('<td></td>');
                            var checkbox = $('<input type="checkbox" name="cchkbox" id="cchkbox" value="' + therapistID + '" >').click(function(){

                                var theraID = $(this).val();

                                //assign therapist to client
                                $.post("api.php/removesave", {clientID: clientID, therapist: theraID}).done(function(data){

                                    //rerun getCurrentTherapists & getATherapists                          
                                    getCurrentTherapists(clientID);
                                    getATherapists(clientID);
                                });
                            });

                            tr.append(td1.append(checkbox)).append(td2.append(therapistName));
                            table.append(tr);
                                    });


                                });
                                };
                                getCurrentTherapists(clientID);
                            });


                        });

                        tr.append(td1.append(checkbox)).append(td2.append(therapistName));
                        table.append(tr);
                    });


                });
                };

function getCTherapists(clientID){
                    $.post("api.php/ctherapists", {clientID: clientID}).done(function(data){

                    var data = JSON.parse(data);

                    var table = $('#tableCurrent');
                    var trHTML = '';
                    $('#tableCurrent').empty();

                    $.each(data, function(i, item){
                        therapistID = item.therapist_id;
                        therapistName = item.therapist_name;

                        var tr = $('<tr></tr>');
                        var td1 = $('<td></td>');
                        var td2 = $('<td></td>');
                        var checkbox = $('<input type="checkbox" name="cchkbox" id="cchkbox" value="' + therapistID + '" >').click(function(){

                            var theraID = $(this).val();

                            //assign therapist to client
                            $.post("api.php/removesave", {clientID: clientID, therapist: theraID}).done(function(data){

                                //refresh the available table which would run $.post("api.php/atherapists) again
                                getCTherapists(clientID);
                                getATherapists(clientID);
                            });


                        });

                        tr.append(td1.append(checkbox)).append(td2.append(therapistName));
                        table.append(tr);
                    });


                });
                };


                getATherapists(clientID);
                getCTherapists(clientID);
佛明·珀多莫(Fermin Perdomo)

http://jsfiddle.net/

$(document).ready(function() {
  var data = [
		{therapist_id: 'id1', therapist_name: 'nam1'},
    {therapist_id: 'id2', therapist_name: 'nam2'},
    {therapist_id: 'id3', therapist_name: 'nam4'},
];
var table = $('#tableAvailable');
 var trHTML = '';
  $.each(data, function(i, item){
      therapistID = item.therapist_id;
      therapistName = item.therapist_name;
      var tr = $('<tr></tr>');
      var td1 = $('<td></td>');
      var td2 = $('<td></td>');
			var checkbox = $('<input type="checkbox" name="achkbox" id="achkbox" value="' + therapistID + '">').click(function(){
      	console.log( $( this ).val() );
        alert($( this ).val());
      });
      tr.append(td1.append(checkbox)).append(td2.append(therapistName));
      table.append(tr);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table id="tableAvailable" >
 
 </table>

o2gxgz9r / 3154 /#&togetherjs = JtIhtUULoM

尝试这样的事情

    var renderTable = function renderTable(data){
    var data = JSON.parse(data);

        var table = $('#tableAvailable');
        var trHTML = '';
        table.empty();

        $.each(data, function(i, item){
            therapistID = item.therapist_id;
            therapistName = item.therapist_name;

            var tr = $('<tr></tr>');
            var td1 = $('<td></td>');
            var td2 = $('<td></td>');
            var checkbox = $('<input type="checkbox" name="achkbox" id="achkbox" value="' + therapistID + '" >').click(function(){

               var theraID = $(this).val();

               //fire ajax call to assign selected therapist to client
               $.post("api.php/assignsave", {clientID: clientID, therapist: theraID}).done(function(data){

               //after assignment refresh the table by running $.post("api.php/atherapists) again
               //maybe making this whole thing a function then recalling it after post
                renderTable(data);
               });


            });

         tr.append(td1.append(checkbox)).append(td2.append(therapistName));
         table.append(tr);
       });

};

$.post("api.php/atherapists", {clientID: clientID}).done(function(data){
        renderTable(data);
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从“隐藏”复选框中获取价值

来自分类Dev

如何从复选框中获取价值?

来自分类Dev

在语义UI中获取Toggle复选框的价值

来自分类Dev

无法从复选框同级输入中获取价值

来自分类Dev

在语义UI中获取Toggle复选框的价值

来自分类Dev

获取价值复选框javascript

来自分类Dev

使用Angular获取复选框的价值

来自分类Dev

从复选框onchange事件获取价值

来自分类Dev

如何从复选框 javascript 中获取结果

来自分类Dev

Javascript:如何从“动态”创建的复选框中获取值

来自分类Dev

.get()无法获取复选框的结果

来自分类Dev

从多个复选框获取$ _POST的问题

来自分类Dev

从角度2打字稿中的复选框中获取价值

来自分类Dev

复选框以在jQuery中过滤结果

来自分类Dev

PHP和JS-选中并输入如何从选定的复选框中获取价值?

来自分类Dev

WordPress高级自定义字段从Repeater中的复选框获取价值

来自分类Dev

如何在对话框中添加复选框并获取价值?

来自分类Dev

WordPress高级自定义字段从Repeater中的复选框获取价值

来自分类Dev

从ajax中选中的复选框获取价值

来自分类Dev

获取多项选择复选框的价值Javascript

来自分类Dev

如果选中,则从纸张复选框获取价值

来自分类Dev

表单POST中的HTML复选框

来自分类Dev

在JavaScript中创建复选框

来自分类Dev

在JavaScript中动态创建复选框

来自分类Dev

在UIWebView中创建动态复选框

来自分类Dev

在JavaScript中动态创建复选框

来自分类Dev

获取symfony中复选框的值

来自分类Dev

获取Swing中的复选框数

来自分类Dev

获取Datagridview中复选框的值?