由json从服务器端返回总和 [英] returning sum from server side by json
问题描述
我通过onkey up事件添加两个文本框值。每次输入数据,都会被提交到服务器,然后结果将由json返回。我得到一个由json文本框(通过一个组合的onchange事件)和另一个文本框已经存在于HTML中,当我改变第二个文本框中的数据(在HTML中),然后总计算,但是当我改变数据第一个文本框(在JavaScript中),那么总数不计算?我哪里错了?第一个和第二个文本框具有相同的类 submitme 。
$(document).ready(function ){
$(。submitme)。keyup(function(){
$ .getJSON('total.jsp',{
firsttextboxname:jQuery(#firsttextbox).val (),secondtextboxname:
jQuery(#secondtextbox).val()
},function(responseData){
var total = responseData.sum;
$(# (#combo).val(total); //显示总共
});
});
$(#combo)。 (){
$ .getJSON('combo.jsp',{
comboboxname:this.value
},function(responseData){
//返回一个文本框文本框)
$(#adiv)。empty()。append(< input type ='text'class ='submitme'id ='firsttextbox'name ='firsttextboxname'/>) ;
});
});
});
html
< div id =adiv>
//在此获取第一个文本框
< / div>
//第二个文本框,其数据也随每次第一个文本框一起被取出
< input type =textid =secondtextboxclass =submitmename = secondtextboxname/> //从这个文本框中取值以计算总和
< input type =textid =thirdtextbox/> //自动填充总和
$ b 服务器端(total.jsp) String a = request.getParameter(firsttextboxname); //获取第一个文本框名称
String b = request.getParameter(secondtextboxname); /获取第二个文本框名称
int c = Integer.parseInt(a); //转换为整数
int d = Integer.parseInt(b); //转换为整数
int e = c + d; //计算总数
JSONObject jsonObj = new JSONObject();
jsonObj.put(sum,e); //将金额发送给客户端
response.setContentType(application / json);
response.getWriter()。write(jsonObj.toString());
当您异步添加组合框(或任何元素)它没有得到keyup事件绑定。你必须像这样使用.live():
$ b
$(。submitme)。live(keyup,function (){
// stuff
});
请参阅 jQuery文档,详细了解.live()的工作方式。
I am adding two text box values by onkey up event. every time data entered, will be submited to the server and then result will be returned by json. I am getting a text box by json(by onchange event of a combo) and another text box is already present in html, when i am changing data in second text box(in html) then total is calculated but when i am changing data in first text box(in javascript) then total is not calculated? Where m i wrong? First and second text boxes have same class submitme.
$(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
server side(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());
When you asynchronously add the combo box (or any element) it does not get the keyup event binding. You have to use .live() like so:
$(".submitme").live("keyup", function() {
// stuff
});
See the jQuery documentation for details on how .live() works.
这篇关于由json从服务器端返回总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!