大家好我正在使用symfony2和jquery进行项目,在实体的添加视图中,我使用了一些jquery代码来执行焦点事件中的一些计算,但是当我将焦点放在输入上时焦点不会被调用,有我的代码:
var timerId = 0;
$('tbody>.ligneproduit input, select').focus(function () {
timerId = setInterval(function() {
var j = 0, totalht = 0, totaltva = 0, totalttc = 0;
// console.log('remise : ' + remise)
for (j=0; j < index; j++) {
// var selectligne = $("#s2id_stock_stockbundle_bonlivaison_lignebl_" + j + "_idarticleentarnat span.select2-chosen").text();
var sum = 0;
totalRemise = 0;
var qte = "stock_stockbundle_bonlivaison_lignebl_" + j + "_qte";
var prix = "stock_stockbundle_bonlivaison_lignebl_" + j + "_pulignebl";
var remise = "stock_stockbundle_bonlivaison_lignebl_" + j + "_remiselignebl";
var total = "stock_stockbundle_bonlivaison_lignebl_" + j + "_totallignebl";
//console.log('index = '+index);
var div = document.getElementById(qte);
if (div) {
if (document.getElementById(qte).value && document.getElementById(prix).value)
{
//console.log('prix = ' + document.getElementById(prix).value);
// Do This
var qtev = document.getElementById(qte).value;
var prixv = document.getElementById(prix).value;
var totalv = document.getElementById(total).value;
var tva = $('span#tva').text();
//console.log('tva ' + document.getElementById(qte).value);
sum = qtev * prixv;
var remiseV = document.getElementById(remise).value;
if (remiseV) {
remiseVal = ((sum / 100) * remise);
totalv = (sum - remiseVal);
} else {
totalv = sum;
}
//console.log('sum = ' + sum);
totalht = (totalht + totalv);
//console.log('totalv = ' + totalv);
//console.log('total ht = ' + totalht);
totaltva = parseFloat(tva / 100) * parseFloat(totalht);
totalttc = parseFloat(totalht + totaltva).toFixed(2);
//console.log('tt = ' + totalttc);
document.getElementById(total).value = parseFloat(totalv, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString();
$('span#totalht').text(parseFloat(totalht, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
$('span#totaltva').text(parseFloat(totaltva, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
$('span#totalttc').text(parseFloat(totalttc, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
}
}
}
}, 100);
});
$('tbody>.ligneproduit input, select').blur(function () {
clearInterval(timerId);
});
,这是必须执行jquery的html代码:
<tbody>
{% for prd in edit_form.lignebl %}
<tr class="ligneproduit{{loop.index}}">
<td style="text-align:center;height: 49px;line-height: 49px">{{loop.index}}</td>
<td class="idarticle">{{ form_widget(prd.idarticle,{ 'attr': {'class': 'form-control'} }) }}</td>
<td class="libellearticle">{{ form_widget(prd.libellelignebl,{ 'attr': {'class': 'form-control'} }) }}</td>
<td class="libelleexternelignebl">{{ form_widget(prd.libelleexternelignebl,{ 'attr': {'class': 'form-control'} }) }}</td>
<td>{{ form_widget(prd.qte) }}</td>
<td>{{ form_widget(prd.pulignebl) }}</td>
<td>{{ form_widget(prd.remiselignebl) }}</td>
<td>{{ form_widget(prd.totallignebl) }}</td>
<td><a class="remove btn red" title="Remove" idx="{{loop.index}}"><i class="fa fa-eraser"></i></a></td>
</tr>
{%endfor%}
请注意,我在开头使用了setinterval并设置了timeout,这是代码:
var index = {{edit_form.lignebl|length}};
var prototype = $('.form_lignefacturefournisseur').data('prototype');
// $(".inputdesible input").prop('disabled', true);
console.log('index ::: ',index);
selectIntervalID = setInterval(selectIntervalFunction, 1000);
intervalID1Remove = setInterval(BtnRemoveInterval, 100);
setTimeout(function() {
clearInterval(selectIntervalID);
clearInterval(intervalID1Remove);
}, 2000);
$('#form_lignefacturefournisseur_btn').on('click', function() {
var oldForm = prototype.replace(/__name__/g, index)
var oldForm1 = oldForm.replace(/ligneproduit/g, "ligneproduit" + index);
var oldForm2 = oldForm1.replace(/idx=""/, 'idx="' + index + '"');
var oldForm3 = oldForm2.replace(/ordre/, ++index);
$('#lignefacturefournisseurtr').before(oldForm3);
$('select').select2();
// index++;
selectIntervalID = setInterval(selectIntervalFunction, 1000);
intervalID1Remove = setInterval(BtnRemoveInterval, 100);
setTimeout(function() {
clearInterval(selectIntervalID);
clearInterval(intervalID1Remove);
}, 2000);
});
function selectIntervalFunction() {
$('.idarticle select').change(function() {
// console.log('ttttttttt');
optionval = $(this).val();
str = $(this).attr('id');
idxline = str.substring(38, 39);
// console.log('idxline ' + idxline);
// console.log('str ' + str);
$('#stock_stockbundle_bonlivaison_lignebl_' + idxline + '_libellelignebl option[value=' + optionval + ']').attr('selected', 'selected');
// $('#stock_stockbundle_bonlivaison_lignebl_' + idxline + '_libelleexternelignebl option[value=' + optionval + ']').attr('selected', 'selected');
$('select').select2();
});
$('.libellearticle select').change(function() {
optionval = $(this).val();
str = $(this).attr('id');
idxline = str.substring(38, 39);
$('#stock_stockbundle_bonlivaison_lignebl_' + idxline + '_idarticle option[value=' + optionval + ']').attr('selected', 'selected');
// $('#stock_stockbundle_bonlivaison_lignebl_' + idxline + '_libelleexternelignebl option[value=' + optionval + ']').attr('selected', 'selected');
$('select').select2();
});
}
任何想法为什么,而不是提前!!