2016-07-15 5 views
0

신용 카드 유형을 자동으로 결정하려고합니다. 아래 코드는 작동하지만 모바일에서는 작동하지 않습니다. 모바일 용 솔루션을 찾으려고했지만 현재로서는 불가능합니다.자동으로 신용 카드 유형 결정

다음은 jQuery의 :

(function($) { 
    $.getCreditCardType = function(val) { 
    if(!val || !val.length) return undefined; 
    switch(val.charAt(0)) { 
     case '4': return 'visa'; 
     case '5': return 'mastercard'; 
     case '3': return 'amex'; 
     case '6': return 'discover'; 
    }; 
    return undefined; 
    }; 
    $.fn.creditCardType = function(options) { 
    var settings = { 
     target: '#credit-card-type', 
    }; 
    if(options) { 
     $.extend(settings, options); 
    }; 
    var keyupHandler = function() { 
     $("#credit-card-type li").removeClass("active"); 
     $("input[id=authorizenet_cc_type]").val(''); 
     switch($.getCreditCardType($(this).val())) { 
     case 'visa': 
      $('#credit-card-type .VI').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('VI'); 
      break; 
     case 'mastercard': 
      $('#credit-card-type .MC').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('MC'); 
      break; 
     case 'amex': 
      $('#credit-card-type .AE').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('AE'); 
      break; 
     case 'discover': 
      $('#credit-card-type .DI').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('DI'); 
      break; 
     }; 
    }; 
    return this.each(function() { 
     $(this).bind('keyup',keyupHandler).trigger('keyup'); 
    }); 
    }; 
})(jQuery); 

답변 : 나는 나는 그가 제공하는 도움을 스콧에 신용을 줄 것이다

<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> 

을 추가했다.

답변

1

키 누르기 대신 keypress를 사용해 보셨습니까?

$(this).keypress(function() { 

}); 

또는 모니터링중인 입력 필드의 입력 이벤트를 사용합니까?

$(this).on('input', function() { 

}); 

편집 - 응답 질문

나는이 상황에서 당신의 코드에서 무슨 일이 있었는지 정말 확실하지 않았다. 나는 물건을 작동시킬 수 있도록 범위 내에서 몇 가지를 변경했습니다.

먼저 HTML을 다음과 같이 가정합니다.

<ul id="credit-card-type"> 
    <li> 
     <form> 
     <input type="text" id="credit-card-type" /> 
     <input type="text" id="authorizenet_cc_type" /> 
     </form> 
    </li> 
</ul> 

그런 다음 JS 당신은 외부 API를 사용하여 시도 할 수있는 입력 이벤트를

 var getCreditCardType = function(val) { 
     if(!val || !val.length) return undefined; 
     switch(val.charAt(0)) { 
      case '4': return 'visa'; 
      case '5': return 'mastercard'; 
      case '3': return 'amex'; 
      case '6': return 'discover'; 
     } 
     return undefined; 
    } 



    var keyupHandler = function() { 
     console.log('test') 
     $("#credit-card-type li").removeClass("active"); 
     $("input[id=authorizenet_cc_type]").val(''); 
     switch(getCreditCardType($(this).val())) { 
     case 'visa': 
      $('#credit-card-type .VI').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('VI'); 
      break; 
     case 'mastercard': 
      $('#credit-card-type .MC').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('MC'); 
      break; 
     case 'amex': 
      $('#credit-card-type .AE').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('AE'); 
      break; 
     case 'discover': 
      $('#credit-card-type .DI').addClass('active'); 
      $("input[id=authorizenet_cc_type]").val('DI'); 
      break; 
     }; 
    }; 



    $('input#credit-card-type').on('input',keyupHandler); 
+0

어떻게 것입니다 내 코드가 작동합니까? 나는 jquery가 좋지 않다. – Jad

+0

기능 코드로 대답을 업데이트했습니다. –

+0

다음은 광산에서 일하는 바이올린입니다. https://jsfiddle.net/pxojzqo1/ –

-1

를 사용하여.

통합하는 것이 쉽지 않을 수 있습니다. 서버로드를 타사로 이전 할 수 있으며 최신 신용 카드 BIN 및 IIN 정보를 얻을 수 있습니다.

가 무료가 제공하는 주변의 여러이며, 계획 지불 :
https://iinapi.com/
https://www.neutrinoapi.com/

+2

https://stackoverflow.com/help/promotion –