請參考fiddle
在文本框中變化的情況,請增加一個功能,如下
$(".animals-input").change(function(e) {
calculator();
});
function calculator()
{
var result = parseInt(0);
$(".animals-input").each(function(){
result = result + parseInt($(this).val());
$("#total-animals").html("Total:"+result);
});
}
UPDATE
中序總重拖你需要添加我的計算器方法在你的droppable事件。所以你的代碼看起來像這樣
$(".animals-box").droppable({
drop: function(event, ui) {
if ($(".animals-box img").length == 0) {
$(".animals-box").html("");
}
ui.draggable.addClass("dropped");
var elem = ui.draggable[0].getAttribute('id').split('-')[1];
animals.splice(animals.indexOf(elem), 1);
var output = [];
for (var i = 0; i < animals.length; i++) {
output.push('<p>' + animals[i] + '<input class="pull-right" value="0" />' + '</p>');
}
$('#list').html(output.join(""));
$(".animals-box").append(ui.draggable);
$('#list').html(output.join(""));
calculator(); // Add this method into your existing code you will get the result.
}
});
});
這是一個樣本Fiddle。請檢查它是否適用於您
我希望這會幫助你。
你需要總數。當你離開任何文本框?我的意思是,當我在貓文本框中寫入2並推送標籤時,它總會給我反射。對? – javidrathod
什麼是可拖動的目的? –
你能解釋(或鏈接到)你以前的問題嗎?我不知道你在做什麼。 – putvande