Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1130349
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 questionOn 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 questionOn 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 01 Answer
Reset to default 1You 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.
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 questionOn 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 questionOn 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 01 Answer
Reset to default 1You 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.
本文标签: Reset counter with jQuery
版权声明:本文标题:Reset counter with jQuery 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749077945a2312598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论