admin管理员组文章数量:1289911
I have a javascript function:
function testCreditCard () {
myCardNo = document.getElementById('ccnumber').value;
myCardType = document.getElementById('ccType').value;
if (checkCreditCard (myCardNo,myCardType)) {
alert ("Credit card has a valid format")
}
else {
alert (ccErrors[ccErrorNo])
};
}
and an input field <input type="text" id="ccnumber" />
and the script function is triggered using an onclick function
<button type="post" id="btn_c" name="btn_c" onclick="testCreditCard();" >Click</button>
Is there anyway I could trigger the function on focus out/right after filling the input field?
Im new to javascript / jquery.Please suggest a solution for this.Thank You
Tried this
<input type="text" id="ccnumber" />
<script> //Script for testCreditCard </script>
<script>$("#ccnumber").on("change", testCreditCard);</script>
<script>
function testCreditCard () {
myCardNo = document.getElementById('ccnumber').value;
myCardType = document.getElementById('ccType').value;
if (checkCreditCard (myCardNo,myCardType)) {
}
else {alert (ccErrors[ccErrorNo])};
}
</script>
</body>
But didnt work.
I have a javascript function:
function testCreditCard () {
myCardNo = document.getElementById('ccnumber').value;
myCardType = document.getElementById('ccType').value;
if (checkCreditCard (myCardNo,myCardType)) {
alert ("Credit card has a valid format")
}
else {
alert (ccErrors[ccErrorNo])
};
}
and an input field <input type="text" id="ccnumber" />
and the script function is triggered using an onclick function
<button type="post" id="btn_c" name="btn_c" onclick="testCreditCard();" >Click</button>
Is there anyway I could trigger the function on focus out/right after filling the input field?
Im new to javascript / jquery.Please suggest a solution for this.Thank You
Tried this
<input type="text" id="ccnumber" />
<script> //Script for testCreditCard </script>
<script>$("#ccnumber").on("change", testCreditCard);</script>
<script>
function testCreditCard () {
myCardNo = document.getElementById('ccnumber').value;
myCardType = document.getElementById('ccType').value;
if (checkCreditCard (myCardNo,myCardType)) {
}
else {alert (ccErrors[ccErrorNo])};
}
</script>
</body>
But didnt work.
Share Improve this question edited Apr 1, 2014 at 19:04 Aminesrine 2,1406 gold badges36 silver badges65 bronze badges asked Apr 1, 2014 at 18:46 PiyaPiya 1,1444 gold badges22 silver badges44 bronze badges 2- Sounds like you're really looking for the change event ? – adeneo Commented Apr 1, 2014 at 18:49
- the blur event will fire when an element loses focus – CSharper Commented Apr 1, 2014 at 18:57
4 Answers
Reset to default 5Bind to the change
or blur
event
$("#ccnumber").on("change", testCreditCard);
why you don't try to call onblur property of the input text?
<input type="text" onblur="testCreditCard()" id="id" name="id" />
when the focus is out of the text the js function is activated
my apologies for my english
You can do
<input type="text" id="ccnumber" onblur ="testCreditCard()" />
or try
$"#ccnumber").on("blur", function(){
testCreditCard();
})
Slightly different binding than the already posted answers, but same idea.
$(document).ready(function() {
$(document).on("blur", "#ccnumber", function() {
testCreditCard();
});
function testCreditCard () {
myCardNo = document.getElementById('ccnumber').value;
myCardType = document.getElementById('ccType').value;
if (checkCreditCard (myCardNo,myCardType)) {
}
else {alert (ccErrors[ccErrorNo])};
}
}
http://jsfiddle/9Gphp/
本文标签: jqueryRun a javascript function on focus outStack Overflow
版权声明:本文标题:jquery - Run a javascript function on focus out - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741402425a2376748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论