[英]how to change value of a button on click
我試圖更改onclick時按鈕的值。 到目前為止,我能夠正常工作,但是當再次單擊時,我希望將其更改回原始值。 有什么建議么?
我的代碼:
{if $activity_count > "0"}
<input onclick="change()" type="button" class="rbutton" value="Show/Hide" id="show_act" />
{literal}
<script>
function change()
{
document.getElementById("show_act").value="Hide/Show";
}
</script>
{/literal}
{else}
{/if}
設置一個標志:
var state = true;
然后修改您的更改功能以切換狀態並根據狀態進行適當的更新:
function change()
{
var textValue = state ? "Hide/Show" : "Show/Hide"; // select the value based on the state
document.getElementById("show_act").value=textValue; // apply the value
state = !state; // toggle the state for the next click
}
這是一個實時示例: http : //jsfiddle.net/7kxA8/
嘗試這個:
document.getElementById("show_act").value = (document.getElementById("show_act").value == "Show") ? "Hide" : "Show";
在change()函數中,這一行代碼就足夠了。
這是一個示例,該示例將具有一個單擊按鈕時會打招呼和再見的按鈕。 將該變量作為document.getElementById(“ show_act”)進行調用有點困難。 在這種情況下,提交按鈕的ID為“ show_act”,因此請確保您跟蹤其名稱。 祝您編程愉快!
<script>
function change(id,text1,text2){
if(document.getElementById(id).value == text1) document.getElementById(id).value = text2;
else document.getElementById(id).value = text1;
}
</script>
<input onClick="change('show_act','Show','Hide');" type="button" class="rbutton" value="Show" id="show_act" />
您可以嘗試以下方法:
function change()
{
var element = getdocument.ElemnetById .("show_act");
element.setAttribute("clicked",true);
if(element.getAttribute("clicked") === 'true'){
element.value="Show/Hide";
element.removeAttribute("clicked")
}else {
element.value="Hide/Show";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.