简体   繁体   English

如何更改单击按钮的值

[英]how to change value of a button on click

I am trying to change the value of a button when onclick. 我试图更改onclick时按钮的值。 so far I am able to get that working but i would like it to change back to the original value when click again. 到目前为止,我能够正常工作,但是当再次单击时,我希望将其更改回原始值。 any suggestions? 有什么建议么?

my code: 我的代码:

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

Set a flag: 设置一个标志:

var state = true;

Then modify your change function to toggle the state and do the appropriate update based on the state: 然后修改您的更改功能以切换状态并根据状态进行适当的更新:

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
}

Here is a live example: http://jsfiddle.net/7kxA8/ 这是一个实时示例: http : //jsfiddle.net/7kxA8/

Try this: 尝试这个:

document.getElementById("show_act").value = (document.getElementById("show_act").value == "Show") ? "Hide" : "Show";

This one line code is enough within change() function. change()函数中,这一行代码就足够了。

Here is an example that will have a button that says hello and goodbye when it is clicked. 这是一个示例,该示例将具有一个单击按钮时会打招呼和再见的按钮。 The variable is a bit challenging to call with the variable as document.getElementById("show_act"). 将该变量作为document.getElementById(“ show_act”)进行调用有点困难。 In this case, the submit button has an ID called "show_act" so be sure you keep track of what you name it. 在这种情况下,提交按钮的ID为“ show_act”,因此请确保您跟踪其名称。 Good luck in your programming! 祝您编程愉快!

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

You can try this:- 您可以尝试以下方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM