[英]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.