JQuery Update data- attribute/value

2020-03-28 16:13发布

I have a DIV that looks like this...utilizing the "data-gauge" attribute.

<div class="uib-justgage gauge no_wrap widget uib_w_6" data-uib="widgets/justgage" data-ver="0" id="uib-justgage-3" data-gauge="{'service method':'none','controller type':'single','data path':'not set','title':'Marry','label':'Marry','value':120,'min':0,'max':200,'hideMinMax':0,'hideValue':0,'hideInnerShadow':1}"
                    data-rpath="null" data-array="not set"></div>

What I would like to do is update a couple of the values via jQuery - namely "value" and "max" to new values.

What would the syntax for that be? I'm new to jQuery and figure it would be something like:

 $.("#uib-justgage-3").something.value=x;
 $.("#uib-justgage-3").something.max=y;

Thanks in advance

3条回答
Ridiculous、
2楼-- · 2020-03-28 16:38
$('#uib-justgage').data({'rpath','Hello'});//sets data-rpath = Hello
$('#uib-justgage').data('rpath');//gets the value assigned to 'data-rpath'
查看更多
Juvenile、少年°
3楼-- · 2020-03-28 16:44

you can do it like this:

$( "#uib-justgage-3" ).data( "foo", 52 );
$( "#uib-justgage-3" ).data( "bar", { myType: "test", count: 40 } );

Then access it like

var foo= $( "#uib-justgage-3" ).data( "foo");
var bar= $( "#uib-justgage-3" ).data( "bar");

Here's a little example, it sets the data, then reads it when you click:

    $("#uib-justgage-3").data("foo", 52);
    $("#uib-justgage-3").data("bar", {
      myType: "test",
      count: 40
    });

    $('#b').click(function() {

      var foo = $("#uib-justgage-3").data("foo");
      var bar = $("#uib-justgage-3").data("bar");

      $('<span>').html("foo: " + foo).appendTo($("#uib-justgage-3"));
      $('<span>').html("<br>bar.myType: " + bar.myType).appendTo($("#uib-justgage-3"));
      $('<span>').html("<br>bar.counte: " + bar.count).appendTo($("#uib-justgage-3"));

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="uib-justgage-3"></div>
<button id="b">Get data</button>

查看更多
看我几分像从前
4楼-- · 2020-03-28 16:55

You could use either $("#uib-justgage-3").data('gauge') or $("#uib-justgage-3").attr('data-gauge') for reading the attribute

and

$("#uib-justgage-3").data('gauge','new value') or $("#uib-justgage-3").attr('data-gauge','new value') for setting the attribute

查看更多
登录 后发表回答