使用jQuery为属性设置新值

时间:2022-11-27 09:51:50

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.

我正在尝试使用attr()设置一个div的自定义属性的新vale。我发现它可以使用。attr(attributeName, value)来完成,但当我尝试时,它不起作用。

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是

<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?

有人能指出我哪里错了吗?

2 个解决方案

#1


43  

Works fine for me

对我来说很不错

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

看到的例子。http://jsfiddle.net/blowsie/c6VAy/

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

确保您的jquery位于$(文档)内。准备好或类似的功能。

Also you can improve your code by using jquery data

还可以使用jquery数据改进代码

$('#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/

这里有一个完整代码的工作示例(差不多)。http://jsfiddle.net/blowsie/c6VAy/3/

#2


29  

It is working you have to check attr after assigning value

它是工作你必须在分配值后检查attr。

LiveDemo

LiveDemo

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

alert($('#amount').attr( 'datamin'));​

#1


43  

Works fine for me

对我来说很不错

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

看到的例子。http://jsfiddle.net/blowsie/c6VAy/

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

确保您的jquery位于$(文档)内。准备好或类似的功能。

Also you can improve your code by using jquery data

还可以使用jquery数据改进代码

$('#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/

这里有一个完整代码的工作示例(差不多)。http://jsfiddle.net/blowsie/c6VAy/3/

#2


29  

It is working you have to check attr after assigning value

它是工作你必须在分配值后检查attr。

LiveDemo

LiveDemo

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

alert($('#amount').attr( 'datamin'));​