[英]Trying to call two functions on the same input field but only calls one
I have two working functions which I want to assign to two inputs: 我有两个工作功能,想分配给两个输入:
<input type="text" id="start0" value="0" name="start[]" onkeyup="displayTotal();"/>
<input type="text" id="end0" value="0" name="end[]" onkeyup="displayTotal();"/>
I would like to be able to use a displayHoras(); 我希望能够使用displayHoras(); onkeyup for those two too. 这两个人的onkeyup也是如此。 (2 many 2s on this thread already). (该线程上已经有2个2)。 when I use displayHoras(); 当我使用displayHoras(); instead of displayTotal(); 而不是displayTotal(); it works, but when I call both of them like this it doesnt: 它有效,但是当我这样称呼他们两个时,它没有:
<input type="text" id="start0" value="0" name="start[]" onkeyup="displayTotal();displayHoras();"/>
<input type="text" id="end0" value="0" name="end[]" onkeyup="displayTotal();displayHoras();"/>
Any help will be welcomed. 任何帮助都将受到欢迎。
I'll share the code of both functions because...who knows? 我将共享这两个函数的代码,因为...谁知道? The problem might be there, right? 问题可能在那里,对吧?
function displayTotal()
{
var tableRows = document.getElementById('budgetTable').getElementsByTagName('tr');
var totalDays = tableRows.length - 3; //Don't count the header rows and the Total rows
var totalPrice = 0;
var price = filterNum(document.getElementById( 'txtPrice' ).value);
var totalField = document.getElementById( 'txtTotal' );
var tempHours = 0;
var tempTotal = 0;
for(var i = 0; i < totalDays; i++)
{
tempHours = document.getElementById("end" + i).value - document.getElementById("start" + i).value;
tempTotal = tempHours * price;
document.getElementById("total" + i).innerHTML = formatCurrency(tempTotal);
totalPrice += tempTotal;
}
totalField.value = formatCurrency(totalPrice*1.21);
}
function displayHoras()
{
var tableRows = document.getElementById('budgetTable').getElementsByTagName('tr');
var totalDays = tableRows.length - 3;
var tempHours = 0;
var tempTotal = 0;
for(var i = 0; i < totalDays; i++)
{
tempHours = document.getElementById("end" + i).value - document.getElementById("start" + i).value;
document.getElementById("totalHoras" + i).innerHTML = tempHours;
}
}
EDIT: I added the functions that create the table below. 编辑:我添加了创建下表的功能。
function keyUpCall() {displayHoras(); displayTotal();}
function addRowToTable()
{
var tbl = document.getElementById('budgetTable');
var lastRow = tbl.rows.length - 4;
var iteration = lastRow;
var entry = iteration - 1; //because we started with day0, etc
var row = tbl.insertRow(lastRow);
// day cell
var cellDay = row.insertCell(0);
cellDay.appendChild(createInput('text','dia' + entry, '', keyUpCall, 'dia' + entry));
// start cell
var cellStart = row.insertCell(1);
cellStart.appendChild(createInput('text','start' + entry, 0, keyUpCall, 'start' + entry));
// end cell
var cellEnd = row.insertCell(2);
cellEnd.appendChild(createInput('text','end' + entry, 0, keyUpCall, 'end' + entry));
// precio unitario
var cellPrecioUnitario = row.insertCell(3);
cellPrecioUnitario.appendChild(createInput('text', null, '$36', null, null));
// total HOras
var cellTotalHoras = row.insertCell(4);
cellTotalHoras.id = 'totalHoras' + entry;
// total cell
var cellTotal = row.insertCell(5);
cellTotal.id = 'total' + entry;
}
function createInput(type, id, value, action, name)
{
var el = document.createElement('input');
el.type = type;
el.id = id;
el.value = value;
el.onkeyup = action;
el.name = name;
return el;
}
At this point, the action is not even attached for some reason. 此时,由于某种原因,甚至没有执行该操作。
Edit2: I SOLVED THE PROBLEM!!! Edit2:我解决了这个问题!!! IUPI!! IUPI! it was this line: var totalDays = tableRows.length - 3; 就是这一行:var totalDays = tableRows.length-3;
In the previous version of this form I was using 3 extra rows, my client got me to add a couple extra ones for Tax and without Tax result. 在此表单的先前版本中,我使用了3个额外的行,我的客户让我添加了几个额外的税款,而没有税款结果。 I changed it to: var totalDays = tableRows.length - 5; 我将其更改为:var totalDays = tableRows.length-5; And that fixed it! 这样就解决了!
您可以创建一个调用两个函数的函数
function function1(){ displayTotal(); displayHoras();}
I'd recommend creating a function that then calls your two functions, eg: 我建议创建一个函数,然后调用您的两个函数,例如:
function handleKeyUp() { // Or `updateDisplay` or some such
displayTotal();
displayHoras();
}
Putting too much text within the onXYZ
attributes is problematic (though I'm not immediately seeing why yours isn't working). 在onXYZ
属性中放置太多文本是有问题的(尽管我没有立即看到为什么您的文本不起作用)。
Off-topic 1 : I'd also suggest hooking up event handlers using DOM2 methods ( addEventListener
on standards-compliant browsers, attachEvent
on IE8 and below) rather than using DOM0 mechanisms like onXYZ
attributes. 主题外1 :我还建议使用DOM2方法(在符合标准的浏览器上使用addEventListener
,在IE8及以下版本上使用attachEvent
)连接事件处理程序,而不是使用onXYZ
属性之类的onXYZ
机制。
Off-topic 2 : A JavaScript library like jQuery , Prototype , YUI , Closure , or any of several others can help smooth over browser differences (and even bugs) like the event attachment stuff above, as well as providing lots of handy utility functionality. 离题2 :像jQuery , Prototype , YUI , Closure或其他几种 JavaScript库可以帮助缓解浏览器之间的差异(甚至是错误),例如上面的事件附件,以及提供许多方便的实用程序功能。 Totally optional, but using one helps you concentrate on what you're actually trying to do, without worrying about slightly different plumbing in various different browsers. 完全可选,但是使用它可以帮助您专注于实际要执行的操作,而不必担心各种不同浏览器中的管道略有不同。
old school javascript ^^ 老派的JavaScript ^^
try this instead of the attribute variant, and use bugzilla, or a javascript debugger 试试这个代替属性变量,然后使用bugzilla或javascript调试器
var checks_keyup = function() {
displayTotal();
displayHoras();
}
var inputs = document.getElementsByTagName('input');
for(var i=0;i<inputs.length;i++) {
if(/^(start|end)/.test(inputs[i].id))
inputs[i].onkeyup = checks_keyup;
}
Try calling second function withing the first function itself, 尝试使用第一个函数本身调用第二个函数,
function displayTotal()
{
\\.....your code
displayHoras();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.