![](/img/trans.png)
[英]How do I call a function that enters text into a read-only text-box by clicking an HTML button?
[英]Can we call a java function from the html text-box?
我正在使用jsp,mysql,使用netbeans和mysql的ajax進行Web項目。 我有3個文本框,其中2個用於接收用戶的輸入。 第三個文本框應顯示2個輸入值的乘積。
這個怎么做? 我應該調用ajax還是可以在3文本框中調用Java函數?
該代碼是
<input type="text" value="" name="quantity"/>
</td><td><input type="text" value="" name="price"/>
</td><td><input type="text" value="" name="total"/>
在名為“ total”的文本框的value屬性中,我可以調用Java函數嗎? 像value="getTotal()"
,但是如果可以的話,我該如何訪問其他兩個值。
否則我應該打電話給ajax嗎?
如果您的要求與所要求的一樣基本,則可以使用簡單的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;
},並將您的html更改為以下內容,以便在發生任何更改時調用javascript函數。
<input type="text" value="" id="quantity" onchange="doTheMath()"/>
建議不要通過服務器調用簡單的數學運算。
您應該使用jQuery。 使用jQuery,您可以根據用戶在其他字段中鍵入的內容來動態設置文本框的值。 我最近在為客戶做的購物籃中實現了此功能。 jQuery還具有一個.ajax()方法,該方法非常易於使用。
查看以下資源:
http://docs.jquery.com/How_jQuery_Works
http://api.jquery.com/category/ajax/
抱歉,我沒有時間編寫編碼答復。 希望這對您有所幫助。
嗨,朋友,您無需執行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>
嗨,朋友,總文本框應該是只讀的,否則您可以使用標簽...。
謝謝
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.