我正在制作一个添加喜欢的项目的功能,当你点击时有一个心形图标,它应该将颜色从灰色变为红色并保存状态。 在我有一个可以保存状态但图像无法适应功能的选择框之前。
SelectBox我可以保存并更改状态:
<select id="priority-select-<?php echo $id ?>" class="form-control" name="priority[<?php echo $id ?>]" title="<?php echo $this->__('Priority') ?>">
<?php foreach($_hlp->getPriorities() as $k=>$v):?>
<option value="<?php echo $k ?>" <?php if ($k == $item->getPriority()){ echo "selected"; } ?>><?php echo $this->htmlEscape($v) ?></option>
<?php endforeach; ?>
</select>
我正在尝试将图像用于图标:
<img id="heart-l-<?php echo $id ?>"
src="<?php if ($item->getPriority() < 1) {echo $this->getSkinUrl('images/heart-m-disabled.png');} else {echo $this->getSkinUrl('images/heart-m.png');} ?>"
alt="" onclick='adicionaFav(this.id)'/>
我的javascript代码:
<script type="text/javascript">
var $jq = jQuery.noConflict();
var heart_m = "<?php echo $this->getSkinUrl('images/heart-m.png') ?>";
var heart_m_disabled = "<?php echo $this->getSkinUrl('images/heart-m-disabled.png') ?>";
var valor;
function adicionaFav(id) {
if(document.getElementById(id).src == "<?php echo $this->getSkinUrl('images/heart-m-disabled.png');?>"){
document.getElementById(id).src = "<?php echo $this->getSkinUrl('images/heart-m.png');?>";
priorityChange(1, id);
$jq('#priority-select-' + id).val('0');
}
else{
document.getElementById(id).src = "<?php echo $this->getSkinUrl('images/heart-m-disabled.png');?>";
priorityChange(0, id);
$jq('#priority-select-' + id).val('1');
}
}
function priorityChange(size,id) {
if(size==1){
$jq("#heart-m-" + id).attr("src", heart_m);
}else{
$jq("#heart-m-" + id).attr("src", heart_m_disabled);
}/*
switch (size) {
case 0: {
$jq("#heart-m-" + id).attr("src", heart_m_disabled);
$jq("#heart-l-" + id).attr("src", heart_l_disabled);
break;
}
case 1: {
$jq("#heart-m-" + id).attr("src", heart_m);
$jq("#heart-l-" + id).attr("src", heart_l_disabled);
break;
}
case 2: {
$jq("#heart-m-" + id).attr("src", heart_m);
$jq("#heart-l-" + id).attr("src", heart_l);
break;
}
}*/
}
/*
function prioritySelect(size, id) {
switch (size) {
case 0: {
priorityChange(size, id);
$jq('#priority-select-' + id).val('0');
break;
}
case 1: {
priorityChange(size, id);
$jq('#priority-select-' + id).val('1');
break;
}
case 2: {
priorityChange(size, id);
$jq('#priority-select-' + id).val('2');
break;
}
}
}*/
</script>
注意:我可以更改心脏的颜色,但升级时无法保存状态。
有人可以告诉我,我该怎么办才能保存最喜欢的状态? 想要将prioridad = 0改为灰心,优先级= 1为红心
答案 0 :(得分:0)
可以按照以下代码解决:
图标:
<img id="heart-m-<?php echo $id ?>"
src="<?php if ($item->getPriority() < 1) {
echo $this->getSkinUrl('images/heart-m-disabled.png');
} else {
echo $this->getSkinUrl('images/heart-m.png');
} ?>"
alt="" onclick='adicionaFav(<?php echo $id ?>)'/>
</div>
Javascript代码:
<script type="text/javascript">
var $jq = jQuery.noConflict();
var heart_m = "<?php echo $this->getSkinUrl('images/heart-m.png') ?>";
var heart_m_disabled = "<?php echo $this->getSkinUrl('images/heart-m-disabled.png') ?>";
var valor;
function adicionaFav(id) {
if (document.getElementById("heart-m-" + id).src == "<?php echo $this->getSkinUrl('images/heart-m-disabled.png');?>") {
$jq("#heart-m-" + id).attr("src", heart_m);
$jq('#priority-select-' + id).val('1');
}
else {
$jq("#heart-m-" + id).attr("src", heart_m_disabled);
$jq('#priority-select-' + id).val('0');
}
}
function priorityChange(size, id) {
if (size == 1) {
$jq("#heart-m-" + id).attr("src", heart_m);
} else {
$jq("#heart-m-" + id).attr("src", heart_m_disabled);
}
}
</script>
希望我能帮助别人! =)