由json从服务器端返回总和

我通过onkey up事件添加两个文本框值。 每次输入数据时,都会被提交到服务器,然后json将返回结果。 我正在通过json获取一个文本框(通过组合的onchange事件)并且另一个文本框已经存在于html中,当我在第二个文本框中更改数据时(在html中)然后计算总数但是当我在更改数据时第一个文本框(在javascript中)然后总计不计算? mi错了? 第一个和第二个文本框具有相同的类提交

$(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(""); }); }); }); 

HTML

 
// getting first text box here
//second text box whose data is also taken along with first text box each time // taking value from this text box to calculate sum // 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()); 

当您异步添加combobox(或任何元素)时,它不会获得keyup事件绑定。 你必须像这样使用.live():

 $(".submitme").live("keyup", function() { // stuff }); 

有关.live()如何工作的详细信息,请参阅jQuery文档 。

在我看来,你喜欢将keyup事件添加到domready中的“.submitme”分类元素,但是firsttextbox还没有在文档上。

它添加在你的#combo的更改事件中,因此它没有获得分配给它的keyup事件。