2

我有一个记录集中的记录列表:

 <?php while($row = mysqli_fetch_array($result_imprint))  
{ ?> 
    <tr><td><input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" value="location_<?php echo $row['imprint_location']; ?>"/> </td>
    <td><?php echo $row['imprint_location']; ?></td>
    <td><?php echo $row['max_size']; ?></td>
    <td><?php echo $row['imprint_type']; ?> </td>
    <td> <?php echo $row['max_colours']; ?></td>
    <td><?php echo $row['setup_fee']; ?></td></tr>
<?php } ?>    

如果有人从其中一条记录中选中一个框,我不知道如何显示 div。最多有六个记录,我已经完成了 div:

    <div class="location_1" id="location_1" style="display: none;">
<br /><hr class="style-seven">
Content Here
</div>

这是我使用的代码,但它似乎不起作用:

$(document).ready(function(){
$('#location').change(function(){
        if(this.checked)
            $('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
        else
            $('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');

    });
});

非常感谢任何帮助。谢谢

4

4 回答 4

1

目前您的 php 代码将被解释为字符串。

尝试将您的代码更改为:

$('#location_['+<?php echo $row["location_id"]; ?>+']').fadein('slow')

并分别用于淡出。

于 2013-10-10T09:39:16.880 回答
1

For starters... IDs have to be unique on your page. So you cannot have a checkbox with id="location_1" and then have a div with also an id of "location_1"

So have checkboxes with id "location_X" and your divs with "div_location_X"

I would also stop using php on javascript, unless you do a ajax call to a php script or you do a loop to write all the javascript for each of the checkboxes, but it would be simpler to:

when checked (use a class to group all the checkboxes together), you can get javascipt to get the id attribute of the checkbox

    <input type="checkbox" id="location_1" name="location_1" class="check"/> </td>

$(document).ready(function(){
$('.check').change(function(){

if((.check).is(":checked"))
    var div_name = $(this).attr('id'); //this should come back with location_1 for the first checkbox
    $('#div_'+div_name+'').fadeIn('slow'); // prepend div_ for the corrent unique id name
else
    $('#div_'+div_name+'').fadeOut('slow');
  });
    });

Use chrome tools console for debugging

于 2013-10-10T10:02:06.917 回答
0

1.div和复选框不能有相同的id,所以我将div id更改为location_div

$(document).ready(function(){
$(":checkbox").click(function () {

if($("input[id^='location_']:checkbox:checked").length>0)
{
    $("#location_div").removeAttr('style');
}
else
{
    $("#location_div").css({"display":"none"});
}


    });
});
于 2013-10-10T12:09:02.780 回答
0
    <tr><td>
<input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" class="check"/> </td>

$(document).ready(function(){
$('#location').change(function(){

        if((.check).is(":checked"))

            $('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
        else
            $('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');

    });
});
于 2013-10-10T09:45:59.363 回答