연락처 양식의 대부분 입력 항목에 대해 실행될 코드 시퀀스가 있습니다. 코드 라인을 위해서이 함수를 함수로 사용하고 싶습니다. 다음 블록의 예입니다반복적 인 코드 블록을 함수로 변환하는 방법
$('#country_code').blur(function() {
var countryCode = $('#country_code').val();
if(validateNumber(countryCode) == true) {
if(countryCode.lastIndexOf('+') != 0) {
countryCode = countryCode.replace('+', '');
$('#country_code').val('+' + countryCode);
}
}
else {
countryCode = '';
$('#country_code').val(countryCode);
}
});
와 나는 같은 함수를 만들려면 다음과 다음과 같은
이function validateElements(elementName, variableName, validationFunction, indexValue, indexPosition) {
$(elementName).blur(function() {
var variableName = $(elementName).val();
if(validationFunction(variableName) == true) {
if(variableName.lastIndexOf(indexValue) != indexPosition) {
variableName = variableName.replace(indexValue, '');
$(elementName).val(indexValue + variableName);
}
}
else {
variableName = '';
$(elementname).val(variableName);
}
});
}
하는 내가 함수를 호출 것 :
validateElements('#country_code', 'countryCode', 'validateNumber', '+', 0);
귀하의 질문은 무엇입니까? – DavidGouge
질문 제목도 혼란 스러웠습니다 – locrizak
질문 : 어떻게이 작업을 할 수 있습니까? 지금 당장은 그렇지 않습니다. – Tony