简体   繁体   中英

Pass Variable into jQuery Plugin Using Switch/Case

I am trying to pass the variable "starter" into a jQuery plugin as one of the config options. The variable will be set using a switch statement where the variable is passed by clicking on an image with a specific id attribute. If image id=1 then starter=1, etc. Right now I'm able to set the variable and display it with an alert statement but don't seem to be able to pass it as a plugin parameter despite the switch statement returning the correct case. What am I missing? Thanks for your help!

     $(document).ready(function()
                    {
                        var starter = '2';
                        $("img").click(function(e){                         
                            e.stopPropagation();
                            switch ($(this).attr("id")) {                              
                                case "1": 
                                starter = 1;
                                break;                      
                                case "2": 
                                starter = 2;
                                break;
                                case "3": 
                                starter = 3; 
                                break;
                                case "4": 
                                starter = 4; 
                                break;
                                case "5": 
                                starter = 5; 
                                break;
                                case "6": 
                                starter = 6; 
                                break;
                            }
                          });
                        $("div").tinycarousel({
                            start: starter
                            });
                        $("img").click(function(){
                        alert(starter)
                       });
                          });

Did you try this:

starter = parseInt($(this).attr("id"));

Or maybe if 0 is a valid default case:

starter = $.isNumeric($(this).attr("id")) ? parseInt($(this).attr("id")) : 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM