我通过onkey up事件添加两个文本框值。每次输入数据,都会被提交到服务器,然后结果将由json返回。我得到一个由json文本框(通过一个组合的onchange事件)和另一个文本框已经存在于HTML中,当我改变第二个文本框中的数据(在HTML中),然后总计算,但是当我改变数据第一个文本框(在JavaScript中),那么总数不计算?我哪里错了?第一个和第二个文本框具有相同的类submitme。从json服务器端返回金额
$(document).ready(function() {
$(".submitme").keyup(function() {
$.getJSON('total.jsp', {
firsttextboxname: jQuery("#firsttextbox").val(), secondtextboxname:
jQuery("#secondtextbox").val()
}, function (responseData) {
var total = responseData.sum;
$("#thirdtextbox").val(total);// displays total
});
});
$("#combo").change(function() {
$.getJSON('combo.jsp', {
comboboxname: this.value
}, function (responseData) {
// returns a text box (first text box)
$("#adiv").empty().append("<input type='text' class='submitme' id='firsttextbox' name='firsttextboxname'/>");
});
});
});
HTML
<div id="adiv">
// getting first text box here
</div>
//second text box whose data is also taken along with first text box each time
<input type="text" id="secondtextbox" class="submitme" name="secondtextboxname"/>// taking value from this text box to calculate sum
<input type="text" id="thirdtextbox"/>// auto filling sum here
服务器端(total.jsp)
String a=request.getParameter("firsttextboxname");// getting first text box name
String b=request.getParameter("secondtextboxname");/ getting second text box name
int c=Integer.parseInt(a);// converting to integer
int d=Integer.parseInt(b);//converting to integer
int e=c+d;// calculating total
JSONObject jsonObj= new JSONObject();
jsonObj.put("sum",e);// sending sum to client side
response.setContentType("application/json");
response.getWriter().write(jsonObj.toString());
是的,它做到了诀窍 – Phillipa 2012-02-21 12:42:02
但是当第一个文本框变为空然后总和仍然在那里没有自动清除为什么?如何清除文本框时清除总数? – Phillipa 2012-02-21 12:43:36
使用jQuery'.on()'如果您使用的是jQuery1.7.1 + – 2012-02-21 16:11:39