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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9515860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:56:39  来源:igfitidea点击:

Can we call a java function from the html text-box?

javascriptmysqlajaxjspnetbeans

提问by Neal

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.

我正在使用 netbeans 和 mysql 使用 jsp、mysql、ajax 做一个 web 项目。我有 3 个文本框,2 个用于接收用户的输入。第三个文本框应显示 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”的文本框的 value 属性中,我可以调用 java 函数吗?类似的东西value="getTotal()",但如果我可以如何访问其他两个值。

Otherwise should i make a ajax call?

否则我应该打一个ajax调用吗?

采纳答案by Lawren Alex

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

谢谢

回答by Pavan Andhukuri

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.

不建议为简单的数学调用服务器。

回答by JeremyS

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.

对不起,我没有时间写一个编码的回复。无论如何,希望这会有所帮助。

回答by Lawren Alex

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