最喜欢的项目 Javascript

Favorite Item Javascript

我正在制作一个功能来添加一个最喜欢的项目,当你点击它时会有一个心形图标,它应该将颜色从灰色变为红色并保存状态。 之前我有一个选择框可以保存状态但是图片无法适配的功能

我可以在其中保存和更改状态的选择框:

 <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 到灰色的心,priority = 1 到红色的心

可以解决,按照下面的代码:

图标:

                        <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>

希望我能帮到别人!! =)