简体   繁体   中英

<img> src attribute not changing using Jquery

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 ?

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

尝试这样做:

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

demo http://jsfiddle.net/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.com/icon-processor/icon40.gif";
                        else if ((ui.value >= 2500) && (ui.value <= 4500))//Moderate: 2500-4500
                            ImageName = "http://aux.iconpedia.net/uploads/14234829766434728.png";
                        else if ((ui.value >= 5000) && (ui.value <= 7000))//Comfortable: 5000-7000
                            ImageName = "http://www.iconempire.com/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);
                    }
                }
            );
        }​

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 );

尝试这个

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

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