admin管理员组文章数量:1327661
I have a hidden input #start thats value is used to display a range of returned data (it is the starting point of an index) each time you click #next I need to increase its value.
$("#next").click(function() {
$("#start").val() + 80;
)};
is this correct? or is there a better way? thx all!
I have a hidden input #start thats value is used to display a range of returned data (it is the starting point of an index) each time you click #next I need to increase its value.
$("#next").click(function() {
$("#start").val() + 80;
)};
is this correct? or is there a better way? thx all!
Share Improve this question edited Sep 17, 2010 at 20:28 KP. 13.7k3 gold badges42 silver badges60 bronze badges asked Sep 17, 2010 at 20:17 Dirty Bird DesignDirty Bird Design 5,55313 gold badges67 silver badges127 bronze badges 05 Answers
Reset to default 6Try this (demo):
$("#next").click(function() {
$("#start").val(function(i,v){
return parseInt(v,10) + 80 || 0;
});
});
You need to convert the value to an integer, add whatever number to it, and save it back:
$("#next").click(function(){
var startElement = $("#start");
var value = parseInt(startElement.val(), 10);
startElement.val(value + 80);
});
Working sample: http://jsfiddle/QZgAf/
All you're doing is retrieving val and adding 80 to it, you're not actually setting it back to the value on the element. To do so try the following:
$("#next").click(function() {
$("#start").val( parseInt($("#start).val()) + 80 );
)};
This is somewhat inefficient though, as you'll be selecting #start twice. Instead I'd cache the #start selection in a variable:
$("#next").click(function() {
var startElement = $("#start");
startElement.val( parseInt(startElement.val()) + 80 );
)};
Here's another way. It accesses the value
attribute directly and uses ~~
to avoid the possible octal or NaN
issues.
$("#next").click(function() {
var start = document.getElementById("start");
start.value = ~~start.value + 80;
});
Or better is to only run the selector for #start
once if the button could be clicked more than once.
// cache "start" outside the handler
var start = document.getElementById("start");
$("#next").click(function() {
start.value = ~~start.value + 80;
});
Use a little closure pattern:
$("#next").click(function cli() {
var v = 0;
return function() {
$("#start").html(v += 80);
};
}());
本文标签: javascriptincrease a hidden input39s value based on click of buttonStack Overflow
版权声明:本文标题:javascript - increase a hidden input's value based on click of button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742229994a2437045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论