admin管理员组

文章数量:1355570

Hi guys I have made a JqueryUI simple slider,on certain value range I make Image URL and then try to change src attribute.

There are four images I'm trying to change.

Check the jsFiddle here.

I think the code is fine but its not working ,I dont know why ?

Hi guys I have made a JqueryUI simple slider,on certain value range I make Image URL and then try to change src attribute.

There are four images I'm trying to change.

Check the jsFiddle here.

I think the code is fine but its not working ,I dont know why ?

Share Improve this question asked Jun 22, 2012 at 12:26 DayTimeCoderDayTimeCoder 4,3325 gold badges40 silver badges64 bronze badges 3
  • ...but the slider is not moving :) (FFox) – Roko C. Buljan Commented Jun 22, 2012 at 12:28
  • Take a look at your firebug/chrome console: postRetrmntImage is not defined $("#" + postRetrmntImage).attr('src', ImageName ); – jantimon Commented Jun 22, 2012 at 12:30
  • Thank you guys I was using undefined variable :/ ,that's so stupid of me :-( – DayTimeCoder Commented Jun 22, 2012 at 12:32
Add a ment  | 

6 Answers 6

Reset to default 2

Replace this:

$("#" + postRetrmntImage).attr('src', ImageName );

to this:

$("#postRetrmntImage").attr('src', ImageName );

The problem is that youre not assigned any postRetrmntImage variable.

See update jsFiddle demo

demo http://jsfiddle/8Sw5J/

Explanation: Please use this: $("#" + sliderId).prop('src', ImageName ); since in your function you are passing the id as sliderId.

Also If I may suggest use .prop good read here: .prop() vs .attr()

Rest demo you can see how it works,

Hope this helps :)

full code

 $(document).ready(function () {
            makeSingleSliderWithImage('postRetrmntMnthlyPaymt', 0, 10000, 0, 500);
        }
        );
        function makeSingleSliderWithImage(sliderId, minimum, maximum, val, steps) {

            //Display label shud have a X appended
            $('#' + sliderId).slider(
                {
                    //range: 'min',
                    min: minimum,
                    max: maximum,
                    step: steps,
                    //starting values for silders
                    value: val,
                    slide: function (event, ui) {
                        //var ImageURL = "Images/";
                        //var ImageName = "";
                        //var ext = ".jpg";

                        if ((ui.value >= 0) && (ui.value <= 2000))//Basic: 0-2000
                            ImageName = "http://www.iconempire./icon-processor/icon40.gif";
                        else if ((ui.value >= 2500) && (ui.value <= 4500))//Moderate: 2500-4500
                            ImageName = "http://aux.iconpedia/uploads/14234829766434728.png";
                        else if ((ui.value >= 5000) && (ui.value <= 7000))//Comfortable: 5000-7000
                            ImageName = "http://www.iconempire./icon-processor/icon40.gif";
                        else if ((ui.value >= 7500) && (ui.value <= 10000))//Luxury:7500-10,000
                            ImageName = "Luxury";
                        //var fullURL = ImageURL + ImageName + ext;
                        //change Image URL
                        $("#" + sliderId).prop('src', ImageName ); //{ src: fullURL });

                        alert($("#" + sliderId).prop('src'));
                        //change Slider Value
                        $("#" + sliderId + "X").text(ui.value);
                    }
                }
            );
        }​

Trying doing this:

 $("#" + postRetrmntImage.toString()).attr('src', ImageName );

Change line...

$("#" + postRetrmntImage).attr('src', ImageName );

to...

$("#postRetrmntImage").attr('src', ImageName );

assign a value to postRetrmntImage then use prop instead of attr see why prop

var postRetrmntImage="postRetrmntImage";
$("#" + postRetrmntImage).prop('src', ImageName );

Try this

$("#postRetrmntImage").attr('src', 'newimage.png');

本文标签: javascriptltimggt src attribute not changing using JqueryStack Overflow