0

I have a html table, the first column is a list of checkboxes (as many as the table rows) all have id="checkbox1", "checkbox2", "checkbox3".... On the click of a button (id="modifyLink") I need to find the first checked checkbox (not bothered if there are any after the first) and then open my modal (id="modModal") and load a php page into it and post the value of the checked checkbox.

This is what I've come out with (obviously not working):

$function(){
    $("#modifyLink").click(function(){
        var checkbox = 'checkbox1';
        for(var i=2; getElementById(checkbox).checked == 1; i++){
            checkbox = 'checkbox'.i;
        }
        if(getElementById(checkbox)==true){
            var id = getElementById(checkbox).val();
            $(#modModal).modal('show');
            $(#modModal).innerhtml = 'load my modal with index.php/trainings/mod passing the id variable possibly in POST';
        });
};

If I'm going about this completely wrong can can someone suggest something completely different?

EDIT:

Ok I've studied in to this a bit more and gone for this:

$(function() {  
  $("#trainingRow").click(function() {  
    var id = $("#trainingRow").attr('data-value');  
    var dataString = 'id='+ id;
    $.ajax({  
        type: "POST",  
        url: "/index.php/trainings/showDetail",  
        data: dataString, 
        dataType: 'json',
        success: function(data) {  
            $("div#detailModal").html(data.html);
            $("div#detailModal").modal('show');
        }  
    });
    return false;
  });  
});

but why doesn't the div#detailModal html update? I've checked what the php function returns through a var_dump and its exactly the HTML code i'm interested in but I don't know why it doesn't work (the returned variable is called html and i json_encode everything in php before I return it). A heads up would be nice, I think I've gone far with a bit of reading :)

4

3 回答 3

0

你的代码很丑……但你不想这样(未经测试)

$(function)({
    $("#modifyLink").click(function(){
        var checkbox = 'checkbox',
            current = null,
            i = 0;
        while (document.getElementById(checkbox + i).length == 1){
            current = $(document.getElementById(checkbox + i));
            if (current.is(':checked'))
            {
              var id = current.val();
              $.post('url', { id: id }, function(content) {
                $('#modModal').modal('show');
                $('#modModal').html(content);
              });
            }
            i += 1;
        }
});
于 2013-09-23T19:22:07.963 回答
0

如果您能够为所有复选框分配一个类,例如“myCheckboxClass”,您可以更轻松地做到这一点。那么你所需要的就是:

$function(){
    $("#modifyLink").click(function(){
        var checkedBox = $(".myCheckboxClass:checked").first();
        var val = checkedBox.val();
        $(#modModal).modal('show');
        $(#modModal).innerhtml = 'load my modal with index.php/trainings/mod passing the id variable possibly in POST';
        });
};

或者,如果您知道表的 id,例如“myTableId”,您可以进行类似的选择,只需将var checkedBox行替换为

var checkedBox = $("#myTableId input:checkbox:checked).first();
于 2013-09-23T19:30:54.443 回答
0

解决了,我的ajax实际上是正确的(第二轮)。问题是即使您对它们进行 json_encode,也无法从 PHP 返回变量。您需要对它们进行 json_encode 并回显它们!

感谢大家的投入!

于 2013-09-29T13:57:26.163 回答