admin管理员组文章数量:1399320
I need to call 2 functions onkeypress out of which first function is return type; and second function is not getting called but if use in reverse order it is working
Requirement:
OnKeyPress, the key should be validated. if number, accept the value in TextBox then copy the value into another textbox; If not a number, then do nothing
Correct Order but 2nd function is not getting called;
<input type="text" name="no_of_units" id="no_of_units" size="5"
onkeypress="return isNumberKey(event);calc_exchange();">
In Reverse order both functions are working. I need the isNumberKey function to be called first.
<input type="text" name="no_of_units" id="no_of_units" size="5"
onkeypress="calc_exchange();return isNumberKey(event);"
Functions :
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
function calc_exchange()
{
var raw1=document.getElementById("no_of_units").value;
document.getElementById("next_no_of_units").value=raw1;
}
I need the following order of function to be called;
- return isNumberKey(event)
- calc_exchange()
I need to call 2 functions onkeypress out of which first function is return type; and second function is not getting called but if use in reverse order it is working
Requirement:
OnKeyPress, the key should be validated. if number, accept the value in TextBox then copy the value into another textbox; If not a number, then do nothing
Correct Order but 2nd function is not getting called;
<input type="text" name="no_of_units" id="no_of_units" size="5"
onkeypress="return isNumberKey(event);calc_exchange();">
In Reverse order both functions are working. I need the isNumberKey function to be called first.
<input type="text" name="no_of_units" id="no_of_units" size="5"
onkeypress="calc_exchange();return isNumberKey(event);"
Functions :
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
function calc_exchange()
{
var raw1=document.getElementById("no_of_units").value;
document.getElementById("next_no_of_units").value=raw1;
}
I need the following order of function to be called;
Share Improve this question edited Jan 20, 2013 at 13:25 logan asked Jan 20, 2013 at 12:50 loganlogan 8,41638 gold badges122 silver badges188 bronze badges 3
- return isNumberKey(event)
- calc_exchange()
- 2 return means done!go back so no statement after return will be executed. – Arpit Commented Jan 20, 2013 at 12:54
- is there any function afterKeyPress ? – logan Commented Jan 20, 2013 at 13:16
- nope! but you can use onkeydown and onkeyup events. – Arpit Commented Jan 20, 2013 at 16:46
4 Answers
Reset to default 3You can call two javascript functions on keypress where first function is a return type function.
<input type="text" name="txtName" id="txtName" size="5"
Onkeypress="return isNumberKey(event,this) && clickButton(event,'imgbtnSearch')">
using && operator, we can call two javascript functions on keypress.
Calling return
will execute what's on the right of it and then exit, so naturally the second function is not getting called.
It's hard to answer without knowing what's inside your functions. But I would suggests you attach event handlers with JavaScript and not inline like you are doing. You will get more control that way.
Look here for how to attach event handlers: http://www.quirksmode/js/events_advanced.html
So you can't call anything after return therefore you can try this. if wrong input raise alert.
<input type="text" name="no_of_units" id="no_of_units" size="5"
onblur=calcexchange() onkeypress="isNumberKey(event)">
....
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
alert("Wrong I/P");
evt.focus()
}
The return
doesn't have to be the first statement. Just call the function in order and return the result later.
<input type="text" name="no_of_units" id="no_of_units" size="5"
onkeypress="var result = isNumberKey(event); calc_exchange(); return result;">
Returning the result of isNumberKey
may prevent the default action, if it returns false the input will be ignored. It seems you want prevent non-numbers and execute calc_exchange
. In this case you should only return false if the input is no number:
if(!isNumberKey(event)) { // validate input
return false; // it's not a number, so prevent default action (do nothing)
}
calc_exchange(); // it was a number, copy the value
The only problem is, at the time you execute calc_exchange
, the entered number hasn't been inserted into the textbox's value. You could listen to other events though, like when the user input was done:
<input type="text" name="no_of_units" id="no_of_units" size="5"
onkeypress="return isNumberKey(event);"
onchange="calc_exchange();" >
本文标签: htmlTwo javascript functions not called when return type function is first calledStack Overflow
版权声明:本文标题:html - Two javascript functions not called when return type function is first called - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744196589a2594770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论