2011-11-01 13 views
1

我有以下腳本:允許值-1上的按鈕和鏈接,當點擊,只是一個時間(首次點擊後),而不禁用按鈕/鏈接

<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> 

如果您單擊按鈕,它會從總金額(3)中扣除1。這同樣適用於負鏈接。現在這一切都很好。

問題是我想讓每個人扣1次,只是一次。不禁用按鈕/鏈接。

例子我需要什麼: 所以我按一下按鈕:3 - > 2。 現在我按一下按鈕再次,什麼都不會發生2只停留2 現在我點擊負鏈接,2 - > 1。 再次點擊鏈接,什麼也沒有發生1停留1

這當然可以混合使用。最主要的是,他們每個人都被允許只做一次(第一次點擊)。

如何做到這一點?

親切的問候, 莫里斯

+0

總體思路:設置後的第一次全球性布爾值,每一次測試。 – Blazemonger

+0

好奇你爲什麼反對禁用/隱藏按鈕/鏈接? – jbabey

+0

我不能禁用按鈕,因爲有其他功能以及 – Maurice

回答

1

由於這個問題是jQuery的下標記,我提供你有辦法做到這一點在jQuery!使用這個可以幫助你從長遠來看,以得到一個更乾淨的代碼和解決這樣的未來的問題;)

演示:http://jsfiddle.net/NtHwN/4/

<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); 
    }); 
}); 
+0

謝謝,我會嘗試這一個,以及 – Maurice

+0

去了這個解決方案,因爲大多數人禁用的onclick(我還需要第一次點擊後)。感謝所有的幫助 – Maurice

0
<input type=button value='-' onclick='javascript:process(-1)' id="button-to-click"> 

刪除單擊處理:

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; 
    } 
1

第一:在onclick屬性中,您不需要javascript: -prefix。

您可以通過點擊移除onclick方法,如下圖所示達到你所期望的行爲,並在此小提琴:http://jsfiddle.net/XPHLU/1/

<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> 
+0

謝謝我會嘗試這一個 – Maurice

1
<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> 
+0

他沒有使用jQuery ... – Steve

+0

不適用於此部分,但其他腳本使用頁面上的jQuery,因此無論如何加載:) – Maurice

+0

@Steve標記爲jQuery問題,所以我給出了選項;)。頁面上的所有解決方案都有相同的解決方案設計,因此它們都可以工作。 – DefyGravity

0

嗯...這個應該工作。 JSFiddle是here

HTML(輕微改變):

<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> 

注:我並不建議使用javascript://

的JavaScript:

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');