나는 다음과 같은 코드가 있습니다jQuery를 및 취급 요소 ID의
function mandatoryField(manF)
{
var fieldId = $(manF).val();
if(fieldId == "")
{
return false;
}
else
{
return true;
}
}
그것은 작동하지 않습니다,하지만이 작업을 수행합니다
function mandatoryField()
{
var fieldId = $("#element_1").val();
if(fieldId == "")
{
return false;
}
else
{
return true;
}
}
는 가정, 내 첫 번째 예에서, mandatoryField는 같은이라고
:
mandatoryField("#element_1")
절대 요소 ID 이름을 변수로 바꿀 때 왜 작동하지 않습니까?
편집 :
가장 최근 코드 - 비 작동 :
: 나는 다음과 같이 할 수있는 모든 일을 다시 작성했습니다 2function isAmExSelected()
{
return $("#creditCardType").val() == "American Express";
}
function containsOnlyDigits(str)
{
return str.match(/[^0-9]/) == null;
}
function validateCCNumber()
{
var ccn = $("#creditCardNumber").val();
var onlyDigits = containsOnlyDigits(ccn);
if(isAmExSelected())
{
return ccn.length == 15 && onlyDigits;
}
else
{
return ccn.length == 16 && onlyDigits;
}
}
function mandatoryField(manF)
{
var fieldId = $("#" + manF).val();
return fieldId != "";
}
function registerValidation(id, validateMethod(), errorMethod)
{
$(id).change(function(){
if(validateMethod() == false)
{
errorMethod();
}
});
}
$(document).ready(function(){
registerValidation("#creditCardNumber", validateCCNumber, function(){alert("Invalid Credit Card Number!")});
$('input[type=text][class=mandatory]').blur(function(){
if (mandatoryField(this.id)) {
alert('Field:' + this.id + ' is mandatory!')
}
});
});
편집
$('input[type=text][class=mandatory]').blur(function(){ if (!($("#" + this.id).val().length)) { alert('Field:' + this.id + ' is mandatory!'); } });
필수 클래스의 텍스트 입력이 흐려지면 함수를 실행합니다. if # foo.val()에 길이가 없습니다 (예 : 텍스트가 없다면 알림을 실행하십시오. 나는 그것이 효과가 있다고 믿지만, 그렇지 않습니다.
어쩌면 당신은 당신의 예를 들어 http://jsfiddle.net/을 만들 수 있습니다. –
당신은 필수 패스 필 수 없다 필수 필드 이름을 –
내 업데이트를 체크 –