Javascript 单击按钮和链接时允许值-1,但只允许一次(第一次单击),而不禁用按钮/链接
我有以下脚本:Javascript 单击按钮和链接时允许值-1,但只允许一次(第一次单击),而不禁用按钮/链接,javascript,jquery,Javascript,Jquery,我有以下脚本: <script language=javascript> function process(v){ var value = parseInt(document.getElementById('v').value); value+=v; document.getElementById('v').value = value; } </script> <input type=test si
<script language=javascript>
function process(v){
var value = parseInt(document.getElementById('v').value);
value+=v;
document.getElementById('v').value = value;
}
</script>
<input type=test size=10 id='v' name='v' value='3'>
<input type=button value='-' onclick='javascript:process(-1)'>
<a id="v" href="#" onclick='javascript:process(-1)'>minus</a>
功能流程(五){
var value=parseInt(document.getElementById('v').value);
值+=v;
document.getElementById('v')。value=value;
}
如果您单击该按钮,它将从总金额(3)中扣除1。这同样适用于负链接。现在一切都很好
问题是,我想让每个人都扣除1,就一次。不禁用按钮/链接
我需要的示例:
所以我点击按钮:3->2。
现在我再次点击按钮,什么也不会发生。
现在我点击负链接,2->1。
再次单击链接,将不会发生任何事情1保持1
当然,这一切都是混合的。最重要的是,他们每个人只允许做一次-1(第一次点击)
如何做到这一点
亲切问候,,
莫里斯
或者多个
<input type=button value='-' onclick='javascript:process(this, -1)'>
第一:onclick属性中不需要
javascript:
-前缀
您可以通过在单击时删除onclick
方法来实现所需的行为,如下所示,并在此小提琴中:
功能过程(v,elem){
if(elem&&elem.onclick){
elem.onclick=null;
}
var value=parseInt(document.getElementById('v').value);
值+=v;
document.getElementById('v')。value=value;
}
$(文档).ready(函数(){
$(“#negativeButton,#v”).one(“click”,{arg:-1},函数(事件){
//.one()注册“单击”事件,执行一次,然后解除绑定。
//停止传播到其他单击事件。
事件。stopImmediatePropagation();
//停止移动到href=“”
event.preventDefault();
过程(event.data.arg,本文件);
});
//或者,您也可以在此之后为相同的按钮注册.click()。one()将仅删除该“one”click事件。
});
功能流程(五){
var value=parseInt(document.getElementById('v').value);
值+=v;
document.getElementById('v')。value=value;
}
因为这个问题是在jQuery下标记的,所以我为您提供了一种在jQuery中完成它的方法!从长远来看,使用它可以帮助您获得更干净的代码,并解决类似这样的未来问题;)
演示:
$(文档).ready(函数(){
$(“.减号”).one(“单击”,函数(){
var value=parseInt($(“#sum”).val();
$(“#sum”).val(值-1);
});
});
嗯。。。这个应该有用。JSFiddle是
HTML(稍作更改):
总体思路:第一次之后设置一个全局布尔值,每次都进行测试。想知道为什么您反对禁用/隐藏按钮/链接?我无法禁用按钮,因为按钮还具有其他功能。谢谢,我将尝试使用此方法,因为大多数其他方法都禁用了onclick(第一次单击后,我仍然需要它)。感谢所有人的帮助。不是这一部分,而是其他脚本在页面上使用jQuery,因此它仍然被加载:)@Steve它被标记为jQuery问题,因此我提供了选项;)。页面上的所有解决方案都具有相同的解决方案设计,因此它们都可以工作。@Maurice.one()事件在任何其他.click()事件之前注册可能很重要。在.one()事件注册后添加其他单击事件的注释为intentional@DefyGravity-从页面中的代码看,没有使用jQuery的证据-document.getElementById()调用等。。我认为这是一个错误的标记。@所有的人都选择了IntoTheVoid的解决方案。谢谢你的帮助!ps我添加了jquery标记,因为我想给你们更多的解决方案选项,而不仅仅是javascript,因为jquery已经加载到我的页面上了。下次会更清楚地说明这一点
function process(v){
var value = parseInt(document.getElementById('v').value);
value+=v;
document.getElementById('v').value = value;
document.getElementById('button-to-click').onclick = null;
}
<input type=button value='-' onclick='javascript:process(this, -1)'>
function process(obj, v){
var value = parseInt(document.getElementById('v').value);
value+=v;
document.getElementById('v').value = value;
obj.onclick = null;
}
<script>
function process(v, elem){
if(elem && elem.onclick){
elem.onclick = null;
}
var value = parseInt(document.getElementById('v').value);
value+=v;
document.getElementById('v').value = value;
}
</script>
<input type=test size=10 id='v' name='v' value='3'>
<input type=button value='-' onclick='process(-1, this);'>
<a id="v" href="#" onclick='process(-1, this)'>minus</a>
<script language=javascript>
$(document).ready(function(){
$("#negativeButton, #v").one("click", {arg: -1}, function(event){
//.one() registers a 'click' event, execute it once, and unbind it.
//stop propagation to the other click events.
event.stopImmediatePropagation();
//stop from moving to href=""
event.preventDefault();
process(event.data.arg, this);
});
//optionally, you can register a .click() after this as well for the same buttons. .one() will only remove that 'one' click event.
});
function process(v){
var value = parseInt(document.getElementById('v').value);
value+=v;
document.getElementById('v').value = value;
}
</script>
<input type=test size=10 id='v' name='v' value='3'>
<input id="negativeButton" type="button" value='-' >
<a id="v" href="#">minus</a>
<input size=10 id="sum" value="3">
<input class="minus" type=button value="-">
<a class="minus" href="#">minus</a>
$(document).ready(function() {
$(".minus").one("click", function(){
var value = parseInt($("#sum").val());
$("#sum").val(value -1);
});
});
<input type=test size=10 id='v' name='v' value='3'></input>
<input id="i" type=button value='Minus'></button>
<a id="l" href="javascript://">minus</a>
function appendEvent(id) {
document.getElementById(id).addEventListener('click', (function() {
var clicked = false;
var inputEl = document.getElementById('v');
return function() {
if (!clicked) {
var value = parseInt(inputEl .value);
inputEl.value = --value;
clicked = true;
};
};
}()));
}
appendEvent('i');
appendEvent('l');