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
Add a ment  | 

4 Answers 4

Reset to default 5

Bind 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