用加号/减号按钮更改类“数据值”?

时间:2017-02-06 11:20:00

标签: javascript input attributes gauge

所以所有教程都展示了如何使用加号和减号按钮更改简单的输入“值”。

这是我的问题,如何更改类“数据值”?

$(document).on('click','.value-control',function(){
    var action = $(this).attr('data-action')
    var target = $(this).attr('data-target')
    var dataValue  = parseFloat($('[id="'+target+'"]').val());
    if ( action == "plus" ) {
      value++;
    }
    if ( action == "minus" ) {
      value--;
    }
    $('[id="'+target+'"]').data("value", (value));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- I want to change the "data-value" value here (it's a meter gauge lots of JS -->

<div class="gauge" id="guagechanger" data-plugin="gauge" data-value="1" data-min-value="1" data-max-value="10" data-stroke-color="#f978a6">
         <div class="gauge-label"></div>
         <canvas width="200" height="150"></canvas>
</div>

<!-- my plus and minus buttons -->

<button type="button" class="btn btn-floating btn-danger value-control" data-action="minus" data-target="guagechanger"><i class="icon wb-minus" aria-hidden="true"></i></button>

<button type="button" class="btn btn-floating btn-danger value-control" data-action="plus" data-target="guagechanger"><i class="icon wb-plus" aria-hidden="true"></i></button>

2 个答案:

答案 0 :(得分:0)

根据jquery .data()

var target = $(this).data('target');

对于更改数据属性:

$(this).data('target', value);

答案 1 :(得分:0)

你真的很亲密,

而不是去target.val()你需要去taget.data(&#39; value&#39;), 而且你对变量名称感到困惑。

这是一个工作片段

&#13;
&#13;
Button button = new Button { Content = "..." };
canvas.Children.Add(button);

int buttonIndex = canvas.Children.IndexOf(button);
int previousElementIndex = buttonIndex - 1;
if(previousElementIndex > -1)
{
    //get the coordinates of the previous element
    UIElement previousElement = canvas.Children[previousElementIndex];
    double x = Canvas.GetLeft(previousElement);
    double y = Canvas.GetTop(previousElement);

    //set the coordinates of the button
    Canvas.SetTop(button, y + 25);
    Canvas.SetLeft(button, x + 25);
}
&#13;
$(document).on('click','.value-control',function(){
    var action = $(this).attr('data-action');
    var target = $(this).attr('data-target');
    var value  = parseFloat($('#'+target).data('value'));
    if ( action == "plus" ) {
      value++;
    }
    if ( action == "minus" ) {
      value--;
    }
    console.log(value);
    $('#'+target).data("value", (value));
})
&#13;
&#13;
&#13;