我们可以从html文本框中调用java函数吗?

时间:2023-01-13 18:10:32

I'm doing a web project using jsp,mysql,ajax using netbeans and mysql. I'm having 3 textboxes, 2 is for taking the input from the user. The 3rd textbox should show the product of 2 input values.

我正在用jsp、mysql、ajax和netbeans做一个web项目。我有3个文本框,2个用于从用户那里获取输入。第三个文本框应该显示两个输入值的乘积。

How to do this? Should i make a ajax call or can i call a java function in the 3 textbox.

如何做到这一点呢?我应该调用ajax还是可以调用3文本框中的java函数。

The code is

代码是

<input type="text" value="" name="quantity"/>
</td><td><input type="text" value="" name="price"/>
</td><td><input type="text" value="" name="total"/>

In the value attribute of the textbox named "total" can i call a java function? something like value="getTotal()",but if i can how can i access the other two values.

在名为“total”的文本框的值属性中,我可以调用java函数吗?类似的价值= " getTotal(),但如果我可以,我如何访问另外两个值。

Otherwise should i make a ajax call?

否则我应该调用ajax吗?

4 个解决方案

#1


1  

Hi friend no need to go java function...You can do simply client side

嗨,朋友,不需要去java函数……你可以做简单的客户端。

  <td><input type="text" value="" name="quantity" onblur="Calculate()"/>
  </td><td><input type="text" value="" name="price" onblur="Calculate()"/>
  </td><td><input type="text" value="" name="total"/>


  <script type="text/javascript">

  function Calculate()
  {

       var txt1 = document.getElementById("quantity");
       var txt2 = document.getElementById("price");
       var txt3 = document.getElementById("total");
       if ((txt1.value != "") && (txt2.value != ""))
       {
            txt3.value = parseInt(txt1.value) * parseInt(txt2.value); 
       }

  }

  </script>

Hi friend one more thing total textbox should be read only or you can use label....

你好朋友一件事总应该只读文本框或您可以使用标签....

thanks

谢谢

#2


2  

If your requirement is as basic as what you are asking, you can do it with simple JavaScript. Add the following to your script

如果你的要求和你要求的一样基本,你可以用简单的JavaScript完成。在脚本中添加以下内容

function doTheMath(){
 var quantity = document.getElementById("quantity").value;
 var price = document.getElementById("price").value;
 var product = parseInt(quantity, 10) * parseFloat(price);
 document.getElementById("total").value = product;

} and change your html to the following to call the javascript function whenever there is a change.

}并将html更改为以下内容,以便在发生更改时调用javascript函数。

<input type="text" value="" id="quantity" onchange="doTheMath()"/> 

Making a server call for simple math is not suggestable.

让服务器调用简单的数学是不可能的。

#3


1  

You should use jQuery. With jQuery you can dynamically set the value of a text box depending on what the user types in other fields. I recently implemented this for a shopping basket I did for a client. jQuery also has a .ajax() method which is quite easy to use.

您应该使用jQuery。使用jQuery,您可以根据其他字段中的用户类型动态设置文本框的值。最近,我为一个客户实现了一个购物篮。jQuery还有一个.ajax()方法,非常容易使用。

Check out these resources:

查看这些资源:

http://docs.jquery.com/How_jQuery_Works

http://docs.jquery.com/How_jQuery_Works

http://api.jquery.com/category/ajax/

http://api.jquery.com/category/ajax/

Sorry I do not have time to write a coded response. Hope this helps anyway.

对不起,我没有时间编写编码响应。希望这可以帮助。

#4


0  

  <HTML>
  <HEAD>
 <TITLE></TITLE>
     <script type="text/javascript">

     function Calculate()
      {

       var txt1 = document.getElementById("FirstNo");
       var txt2 = document.getElementById("SecondNo");
       var txt3 = document.getElementById("Output");
       if ((txt1.value != "") && (txt2.value != ""))
       {
            txt3.value = parseInt(txt1.value) * parseInt(txt2.value); 
       }

      }

    </script>

 <input id="FirstNo" type="text" value="" onblur="Calculate()" style="width:50px"/> * 
 <input id="SecondNo" type="text" value="" onblur="Calculate()" style="width:50px"/>
 <input id="Output" type="text" style="width:50px"/>
 </FORM>
 </BODY>
 </HTML>

#1


1  

Hi friend no need to go java function...You can do simply client side

嗨,朋友,不需要去java函数……你可以做简单的客户端。

  <td><input type="text" value="" name="quantity" onblur="Calculate()"/>
  </td><td><input type="text" value="" name="price" onblur="Calculate()"/>
  </td><td><input type="text" value="" name="total"/>


  <script type="text/javascript">

  function Calculate()
  {

       var txt1 = document.getElementById("quantity");
       var txt2 = document.getElementById("price");
       var txt3 = document.getElementById("total");
       if ((txt1.value != "") && (txt2.value != ""))
       {
            txt3.value = parseInt(txt1.value) * parseInt(txt2.value); 
       }

  }

  </script>

Hi friend one more thing total textbox should be read only or you can use label....

你好朋友一件事总应该只读文本框或您可以使用标签....

thanks

谢谢

#2


2  

If your requirement is as basic as what you are asking, you can do it with simple JavaScript. Add the following to your script

如果你的要求和你要求的一样基本,你可以用简单的JavaScript完成。在脚本中添加以下内容

function doTheMath(){
 var quantity = document.getElementById("quantity").value;
 var price = document.getElementById("price").value;
 var product = parseInt(quantity, 10) * parseFloat(price);
 document.getElementById("total").value = product;

} and change your html to the following to call the javascript function whenever there is a change.

}并将html更改为以下内容,以便在发生更改时调用javascript函数。

<input type="text" value="" id="quantity" onchange="doTheMath()"/> 

Making a server call for simple math is not suggestable.

让服务器调用简单的数学是不可能的。

#3


1  

You should use jQuery. With jQuery you can dynamically set the value of a text box depending on what the user types in other fields. I recently implemented this for a shopping basket I did for a client. jQuery also has a .ajax() method which is quite easy to use.

您应该使用jQuery。使用jQuery,您可以根据其他字段中的用户类型动态设置文本框的值。最近,我为一个客户实现了一个购物篮。jQuery还有一个.ajax()方法,非常容易使用。

Check out these resources:

查看这些资源:

http://docs.jquery.com/How_jQuery_Works

http://docs.jquery.com/How_jQuery_Works

http://api.jquery.com/category/ajax/

http://api.jquery.com/category/ajax/

Sorry I do not have time to write a coded response. Hope this helps anyway.

对不起,我没有时间编写编码响应。希望这可以帮助。

#4


0  

  <HTML>
  <HEAD>
 <TITLE></TITLE>
     <script type="text/javascript">

     function Calculate()
      {

       var txt1 = document.getElementById("FirstNo");
       var txt2 = document.getElementById("SecondNo");
       var txt3 = document.getElementById("Output");
       if ((txt1.value != "") && (txt2.value != ""))
       {
            txt3.value = parseInt(txt1.value) * parseInt(txt2.value); 
       }

      }

    </script>

 <input id="FirstNo" type="text" value="" onblur="Calculate()" style="width:50px"/> * 
 <input id="SecondNo" type="text" value="" onblur="Calculate()" style="width:50px"/>
 <input id="Output" type="text" style="width:50px"/>
 </FORM>
 </BODY>
 </HTML>