【问题标题】:Change value of a textbox, compared to another text box value with a ratio更改文本框的值,与另一个具有比率的文本框值相比
【发布时间】:2012-07-18 13:30:10
【问题描述】:

我正在研究 ASP.NET C#。

使用 Javascript,当另一个文本框的值以 3:2 的比例变化时,我需要更改一个文本框的值。

例如,如果文本中的值为 150,则其他文本框的值应为 100。

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

    试试这个,

    Javascript:

    <script type="text/javascript">
        function fill() {
            document.getElementsByName('txt2')[0].value) = (document.getElementsByName('txt1')[0].value) * (2 / 3);
        }
    </script>
    

    正文标签中的代码:

    <form id="form1" runat="server">
    <div>    
        <%--The text boxes must have numeric values--%>
        <asp:TextBox ID="txt1" runat="server" onchange="javascript:fill();"></asp:TextBox>
    
        <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    </div>
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      使用类似的东西

      <!DOCTYPE html>
      <html>
      <head>
      <script type="text/javascript">
      function change() {    
          var t1Val = (document.getElementById("t1")).value;
          (document.getElementById("t2")).value = ((t1Val * 2) / 3);
      };
      </script>
      </head>
      <body>
      
      <input id="t1" type="text" onchange="change()"/>
      <input id="t2" type="text"/>
      
      </body>
      </html>
      

      如果您需要任何验证,您可以在 change() 函数中编写。

      http://jsfiddle.net/bdd63/1/

      【讨论】:

      • 请在您的回答中提供更多上下文。不仅仅是一个链接
      • 我想在 c# 中输入 id。但我无法得到它。我怎样才能得到输入ID。如果我给 runat="server" 它会正常工作。但 javascript 不起作用。
      【解决方案3】:

      您可以在文本框上使用onchangeonkeyup 事件

      将以下 java-script 函数添加到您的 html 中

      function ChangeValue() {
          if (document.getElementById('txtval1').value) {
              document.getElementById('txtval2').value=document.getElementById('txtval1').value*2/3;
          }
      }   
      

      然后在你的html中添加两个id为txtval1,txtval2的文本框,在第一个中添加onkeyup="ChangeValue();

      【讨论】:

        猜你喜欢
        • 2013-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多