Is it possible to search for json result using jQuery

What I'm trying to achieve is to output a json list containing a list of Css classes and their corresponding URL entries, i.e.

var jsonList = [{"CSSClass":"testclass1","VideoUrl":"/Movies/movie.flv"},{"CSSClass":"testclass2","VideoUrl":"/Movies/movie2.flx"}]; //]]>

The foreach element in the list. I am adding a click event to the class ...

$.each(script, function() {
        $("." + this.CSSClass, "#pageContainer").live('click', function(e) {
            videoPlayer.playMovie(this);
            return false;
        });
    });

I am wondering if I can somehow get the corresponding url from jsonlist, without having to repeat all of them, look for CSSClass or add link url as attribute?

+3
source share
2 answers

you can add the Index and Item parameter to the callback function in the $ .each method.

$.each(script, function(i, item) { 
   $("." + item.CSSClass, "#pageConainer").live("click", function() {
       videoPlayer.playMovie(item.VideoUrl);
       return false;
   });
});
  • "i" will be the counter of each iteration in the json object
  • "item"
+2

, , . - :

$.each(script, function() {
    var vid = this;
    $("." + vid.CSSClass, "#pageContainer").live('click', function(e) {
        videoPlayer.playMovie(vid.VideoUrl);
        return false;
    });
});
+1

Source: https://habr.com/ru/post/1744452/


All Articles