简体   繁体   中英

How to link a progress bar with .click event jquery

I want to create a voting system. How can I link the .click event with the progress bar so that when a user clicks the button the progress bar updates in sync.

Here's my code

HTML

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>

<div class="content">
<h3 class="title">Who's better ?</h3>
<ul>
    <li class="option" id="option_1" >
            Messi
        <p id="score_1">0</p>
        <div class="progressbar_1">
        </div>
        <br>
    </li>
    <br>
    <li class="option" id="option_2" >
        Ronaldo
        <p id="score_2">0</p>
        <div class="progressbar_2"></div>
        <br>
    </li>
    <br>
</ul>
</div>

Jquery

$('#option_1').click(function() {
$('#score_1').html(function(i, num) { return num*1+1});
});

$('#option_2').click(function() {
$('#score_2').html(function(i, num) { return num*1+1 });
});

$(function() {
$( ".progressbar_1" ).progressbar({value: 50});
});

$(function() {
$( ".progressbar_2" ).progressbar({value: 50});
});

http://jsfiddle.net/h16nhz5c/5/

You need to re-call progressbar() upon voting. Something like:

HTML:

<ul>
    <li class="option" id="option_1">
        Messi

        <p class="score" id="score_1">0</p>

        <div class="progressbar">
        </div>
    </li>

    <li class="option" id="option_2">
        Ronaldo

        <p class="score" id="score_2">0</p>

        <div class="progressbar">
        </div>
    </li>
</ul>

Javascript:

$('.option').click(function() {
    var $this = $(this);

    // store voting value in data-voting
    if (!$this.data('voting'))
        $this.data('voting', 0);

    var voting = parseInt($this.data('voting'), 10);
    voting++;

    $this.data('voting', voting);
    $this.find('.score').html(voting);
    $this.find('.progressbar').progressbar({value: voting});
});

See Fiddle

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