简体   繁体   English

使用Javascript更改按钮上的文本

[英]Change Text on Button with Javascript

I just started with ASP.NET and C# a week ago and its going the right way. 一周前,我刚开始使用ASP.NET和C#,它的运行方式正确。 At my journey I ran in to a problem with changing the Box Label when clicking on it. 在旅途中,我遇到了一个问题,即单击它时更改“框标签”。

I found some examples on this site and tried to implement it, but somehow it is not working. 我在此站点上找到了一些示例,并尝试实现它,但是不知何故它不起作用。

My head looks like: 我的头看起来像:

<head runat="server">
    <title></title>
  <script src="/js/jquery-1.10.2.js" type="text/javascript"></script>  
  <script type="text/javascript">

      $('button').click(function () {
          $(this).text(function (i, old) {
              return old == '+' ? '-' : '+';
          });
      });

  </script> 
</head>

and my body looks like: 我的身体看起来像:

<body>

        <button>+</button>

</body>

But this button is not changing from + to - when im clicking on it. 但是,当我单击它时,此按钮并未从+更改为-。

I tried to put on jsfiddle, and here it is working. 我试图穿上jsfiddle,并且在这里工作。

http://jsfiddle.net/fVpkm/114/ http://jsfiddle.net/fVpkm/114/

can you tell me what is wrong with my very simple form? 您能告诉我我非常简单的表格有什么问题吗?

Your Sincerely 忠实于你的

KP 知识点

In your fiddle, on the left side, it says onDomready. 在您的提琴中,在左侧显示onDomready。 So you don't have to use document.ready. 因此,您不必使用document.ready。 But in the real code, you should wait for the dom to load. 但是在实际代码中,您应该等待dom加载。

<head runat="server">
    <title></title>
  <script src="/js/jquery-1.10.2.js" type="text/javascript"></script>  
  <script type="text/javascript">
 $(document).ready(function(){ 
      $('button').click(function () {
          $(this).text(function (i, old) {
              return old == '+' ? '-' : '+';
          });
      });
});
  </script> 
</head>

As already mentioned by Ashish, your fiddle works perfectly. 正如Ashish已经提到的那样,您的提琴效果很好。 The only problem here would be the correct declaration. 唯一的问题是正确的声明。

Try wrapping the script in the DOM load event: 尝试将脚本包装在DOM load事件中:

$(document).ready(function () {
  /* code here */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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