最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Reset counter with jQuery

matteradmin10PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

On WordPress I'm switching between categories of posts with a dropdown, and have a "load more" button that loads more posts into the selected category. Each time the button is pressed, a variable is updated, so WordPress knows whether to load the 2nd, 3rd, 4th tranche of posts in that category.

The counter ought to reset when the dropdown is changed though - and I can't figure out how to do it.

To simplify, if you have:

<select id="selection">
  <option value="foo"></option>
  <option value="bar"></option>
</select>

<button id="button"></button> 

I want to to get a string of alerts - one per button click - saying "foo1", "foo2" // "bar1", "bar2" // "foo1", "foo2", "foo3".

My code doesn't quite work though - after a while changing the dropdown I get multiple alerts for a single button click.

$(document).ready(function(){
    $("#selection").on('change', function(){
         myval = $("#selection").val();
         count = 0; 
         $("button").on('click', function(){
             count++;
             alert(myval + count);
        })
     })
 })

How can I completely wipeout my count value when the selector changes?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

On WordPress I'm switching between categories of posts with a dropdown, and have a "load more" button that loads more posts into the selected category. Each time the button is pressed, a variable is updated, so WordPress knows whether to load the 2nd, 3rd, 4th tranche of posts in that category.

The counter ought to reset when the dropdown is changed though - and I can't figure out how to do it.

To simplify, if you have:

<select id="selection">
  <option value="foo"></option>
  <option value="bar"></option>
</select>

<button id="button"></button> 

I want to to get a string of alerts - one per button click - saying "foo1", "foo2" // "bar1", "bar2" // "foo1", "foo2", "foo3".

My code doesn't quite work though - after a while changing the dropdown I get multiple alerts for a single button click.

$(document).ready(function(){
    $("#selection").on('change', function(){
         myval = $("#selection").val();
         count = 0; 
         $("button").on('click', function(){
             count++;
             alert(myval + count);
        })
     })
 })

How can I completely wipeout my count value when the selector changes?

Share Improve this question asked Dec 21, 2018 at 11:21 JohnGJohnG 3443 silver badges17 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

You can do this with each function by creating the array and push the selected value into it.

Html

<select id="selection">
  <option value="foo"></option>
  <option value="bar"></option>
</select>

Jquery

 var arrayOfValues = new Array();
    $("select").each(function(i,obj){
     count = 0; 
        $(obj).change(function() {
             $("select").each(function(i,obj){
             count++;
                arrayOfValues.push($(obj).find(":selected").val()+count);         
             });
            alert(arrayOfValues);
        });
    });

Hope this will help you and also let me know the result.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far