Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

javascript - Setting new value for an attribute using jQuery

I am trying to set a new vale of a custom attribute of a div using attr(). I found out it can be done using .attr( attributeName, value ), but when I try it it's not working.

Here is the part of my code I am interested in:

$('#amount').attr( 'datamin','1000')

and the div that has the custom attribute is

<div id="amount" datamin=""></div>


Here is the whole example:

$(function() {
    $( "#slider-range" ).slider({
        range: true,
        min: <?php echo $min_val;?>,
        max: <?php echo $max_val;?>,
        values: [ <?php echo $min_val;?>, <?php echo $max_val;?> ],
        slide: function( event, ui ) {
            $( "#amount" ).html( "<?php echo $currency_chk_i;?>" + ui.values[ 0 ] + " - <?php echo $currency_chk_i;?>" + ui.values[ 1 ] );

            $('#amount').attr( 'datamin','1000');

        },
        change: function(event, ui) {
            // when the user change the slider
        },
        stop: function(event, ui) {
        alert(ui.values[0]);
            // when the user stopped changing the slider

        }
    });
    $( "#amount" ).html( "<?php echo $currency_chk_i;?>" + $( "#slider-range" ).slider( "values", 0 ) +
        " - <?php echo $currency_chk_i;?>" + $( "#slider-range" ).slider( "values", 1 ) );

});


Can anyone point out where I am wrong or any other way out?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Works fine for me

See example here. http://jsfiddle.net/blowsie/c6VAy/

Make sure your jquery is inside $(document).ready function or similar.

Also you can improve your code by using jquery data

$('#amount').data('min','1000');

<div id="amount" data-min=""></div>

Update,

A working example of your full code (pretty much) here. http://jsfiddle.net/blowsie/c6VAy/3/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...