onkey upイベントで2つのテキストボックス値を追加しています。データが入力されるたびにサーバーに送信され、結果はjsonによって返されます。私は(コンボのonchangeイベントで)jsonによってテキストボックスを取得しています。もう1つのテキストボックスはすでにhtmlにあります.htmlの2番目のテキストボックスでデータを変更すると合計が計算されますが、最初のテキストボックス(javascriptで)合計が計算されていない?どこが間違っていますか?最初と2番目のテキストボックスは同じクラスsubmiteです。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
をしましたが、最初のテキストボックスが空になると自動的にクリアされずにまだ残りますか?テキストボックスがクリアされたら、どのようにクリアすることができますか? – Phillipa
jQuery1.7.1 +を使用している場合は、jQuery '.on()'を使用してください。 –