我确信这很简单但我已经尝试了很久了。 我有一个foreach循环,它迭代一个对象数组,并在一个带有提交按钮的html表中显示它们,单击这个按钮时会调用ajax来显示同一页面中的一些数据。
这一切都正常,但它只传递foreach循环中数组中最后一个对象的值,而不是该表行中显示的值。 我试图使用一个计数器或设置和id为但不确定如何获得该特定值并传递它。
以下是我的代码:
<?php
// No direct access
defined('_JEXEC') or die; ?>
<div class="status shelterbuddydog" >
<table class="profile_table">
<?php foreach ($animalDetails as $profile):?>
<tr>
<td><?php echo $profile->photo;?></td>
<td><span class="profile">Name:<?php echo $profile->name;?></span><br/>
<span class="profile">Sex: <?php echo $profile->sex;?></span></<br/>
<span class="profile">Age: <?php echo $profile->age;?></span><br/>
<span class="profile">Breed: <?php echo $profile->breed;?></span><br/>
<span class="profile">Animal Id: <?php echo$profile->animalId;?></span><br/>
<span class="profile">Location: <?php echo $profile->shelter;?></span><br/>
<?php $profile->summary;?>
<input type="submit" id="summary" name="summary" value="VIEW MY PROFILE">
</input></td>
</tr>
<?php endforeach;
</table>
<?php
// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
$(document).on('click', '#summary', function () {
var profileSummary = {};
profileSummary['photo'] = '$profile->photo';
profileSummary['name'] = "$profile->name";
profileSummary['sex'] = "$profile->sex";
profileSummary['age'] = "$profile->age";
profileSummary['breed'] = "$profile->breed";
profileSummary['shelter'] = "$profile->shelter";
profileSummary['animalId'] = "$profile->animalId";
profileSummary['summary'] = "$profile->summary";
request = {
'option' : 'com_ajax',
'module' : 'shelterbuddydog',
'data' : profileSummary,
'format' : 'raw'
};
$.ajax({
type : 'GET',
data : request,
success: function (response) {
$('.status').html(response);
}
});
return false;
});
})(jQuery)
JS;
$doc->addScriptDeclaration($js);
?>
</div>
感谢您提供任何帮助。
答案 0 :(得分:1)
添加像这样的隐藏字段
<?php foreach ($animalDetails as $profile):?>
<tr>
<td><?php echo $profile->photo;?></td>
<td><span class="profile">Name:<?php echo $profile->name;?></span><br/>
<span class="profile">Sex: <?php echo $profile->sex;?></span></<br/>
<input type="submit" class="summary" name="summary" value="VIEW MY PROFILE">
</input>
<input type="hidden" class="name" value="<?php echo $profile->name;?>" />
<input type="hidden" class="sex" value="<?php echo $profile->sex;?>" />
</td>
</tr>
然后
$(document).on('click', '.summary', function () {
var $tr=$(this).parents('tr');
var profileSummary = {};
profileSummary['name'] = $tr.find('.name').val();
profileSummary['sex'] = $tr.find('.sex').val();
request = {
'option' : 'com_ajax',
'module' : 'shelterbuddydog',
'data' : profileSummary,
'format' : 'raw'
};
或者您可以使用按钮以及html5数据属性
来绑定信息<input class="summary" data-name="<?php echo $profile->name;?>" />