dsvyc66464
2013-04-15 20:37
浏览 30
已采纳

将jquery函数从表单提交转换为php数组结果

I'm attempting to take the ImageResolver plugin and adapt it to work with a php array.

Stripping the code to this returns the image without a form:

$(function(){

    var url = $('#url').val();
    ImageResolver.resolve(url, function(image){
        if (image) {
            $('#result').html('<img src="' + image + '" alt="">');
        } else {
            $('#result').html('<h2>No image found</h2>');
        }

});
});

I want to adapt it to work within a php foreach loop. results would be replaced on the next class='result' div. IE: after the page has loaded the urls from the query, the function will parse the url and return image link if one is found. I'm guessing I need to use (each) or this(), but I can't figure it out.

can someone point me in the right direction?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duandanbeng1829 2013-04-16 00:12
    最佳回答
            <script src="ImageResolver/URI.min.js"></script>
            <script src="ImageResolver/ImageResolver.js"></script>
    
    
    
        <?
            $javascriptarray = 'var urls = [';
            $counter=0;
            foreach (array('http://www.apple.com/','http://github.com/','http://www.test.com/') as $url) 
            {
                if ($counter++ > 0) $javascriptarray .= ',';
                $javascriptarray .= '"'.$url.'"';
            }
            $javascriptarray .= '];';   
        ?>
    <script>    
    <?=$javascriptarray?>
    
    //The ImageResolver will try all the resolvers one after the other
    //in the order of their registration
    
    //Resolvers that guess the image URL
    ImageResolver.register(new FileExtensionResolver());
    ImageResolver.register(new ImgurPageResolver());
    ImageResolver.register(new NineGagResolver());
    ImageResolver.register(new InstagramResolver());
    
    //Resolvers that need extra ajax requests
    ImageResolver.register(new ImgurAlbumResolver());
    ImageResolver.register(new OpengraphResolver());
    ImageResolver.register(new WebpageResolver());
    
    //Some jQuery code to make the demo work
    //Use a crossdomain proxy (required by some plugins)
    $.ajaxPrefilter('text', function(options) {
        options.url = "http://furious-stream-4406.herokuapp.com?src=" + encodeURIComponent(options.url);
    });
    
    $(function(){
    
    
    var length = urls.length,
    url = null;
        for (var i = 0; i < length; i++) {
                url = urls[i];
                ImageResolver.resolve(url, function(image){
                if (image) {
                    $('#result').append('<img src="' + image + '" alt=""><br>');
                } else {
                    $('#result').append('<h2>No image</h2>');
                    //$('#result').append('<h2>No image found for ' + url + '</h2>');
                }
            }); 
        }
    }); 
    
    </script>   
    

    Watch out cause ImageResolver.resolve() works asynchrone you can get unexpected results. Call ImageResolver.resolve() again before the previous call has finished will change url in $('#result').append('<h2>No image found for ' + url + '</h2>'); to the url of your last call by example. To prevent this you need to initialize a new Resolver in the for-loop. see: Javascript prototypes and instance creation

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题