weixin_33739523 2011-04-28 10:04 采纳率: 0%
浏览 25

jQuery PHP / MySQL图像管理器

I'm working on a CMS at the moment and have been looking for a suitable Jquery/Php image manager but can't find anything that really suits my needs. So I've decided to have a go at building the image manager myself. The bad news is I'm not particularly up to speed with my Jquery...actually I know very little. But I'm working my way through it.

The end result will display an unordered list of images.

<ul>
<li><a href="#" id="imgid"><img src="imgsrc" /></a></li>
<ul>

When the user clicks on an image the ID of that image is sent to a Jquery function and stored in an array. The image is highlighted with a border added by addclass(). If the user clicks on a subsequent image that image ID is also sent to the Jquery array and the second image is also highlighted.

This continues until the user doesnt click on any further images. Naturally if the user clicks on an image that is already contained in the Jquery array then it is removed and the highlighted effect is also removed.

Following this the user will select a catalogue item from a select menu that sits below the image list. The ID of that item is also passed to Jquery.

When the user clicks submit the Jquery array and select ID are sent to a PHP script that updates a MySQL table, linking the image ID to the catalogue item ID.

Where I'm up to currently is...I have a lovely unordered list of images.

My questions are

  1. How do I send the image id to the jquery array, and subsequently remove that id if the same image is clicked again
  2. How do I send the Jquery array to the PHP script to be executed

I have got some of this working in code snippets at the moment, but as Jquery is far from my forte Im just rinsing and repeating until I get the code right. Any help would be great.

Mark

So Ive been trying this code snippet, but I think i could be written better.

$(document).ready(function() {
    $("img").click(function(event) {
        var test = (event.target.id);
        if($('img[id=' + test + ']').is('.error'))
            $('img[id=' + test + ']').removeClass("error");
        else
            $('img[id=' + test + ']').addClass("error");
    });
});
</script>

Surely there's another way to target the img id without having to use 'img[id=' + test + ']'

Mark

Edited again

$(document).ready(function() {
    $("img").click(function(event) {
        var img_id = (event.target.id);
        var imgArray = new Array();
        if($('img[id=' + img_id + ']').is('.selected')) {
            $('img[id=' + img_id + ']').removeClass("selected");
            $('span[id=check_' + img_id + ']').removeClass("check-ok");
            removeArray();
            }
        else {
            $('img[id=' + img_id + ']').addClass("selected");
            $('span[id=check_' + img_id + ']').addClass("check-ok");
            addArray();
            }
        function removeArray() { 
            alert('Removing from the array');
            }
        function addArray() {
            alert('Adding into the array');
            }
        });
    });

Trying to figure out how to insert the images into the array, and then remove them from the array.

  • 写回答

1条回答 默认 最新

  • weixin_33705053 2011-04-28 23:58
    关注

    I'm not really sure what all the bits of your code are doing, but something like this should at least make it a bit more DRY:

    function showMessage(selected) {
        if( true === selected )
          {
          alert('Removing from the array');
          }
        else
          {
          alert('Adding to the array');
          }
    }
    
    $(document).ready(function() {
        $('img').click(function(event, ui)
          {
          $(this).toggleClass('selected');
          var id = $(this).attr('id');
          $('#check_' + id).toggleClass('check-ok');
          showMessage( $(this).hasClass('selected') );
          });
    });
    

    And in jsfiddle (although I replaced img with div for speed).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog