2015-01-08 2 views
0

제 양식에 모든 필드가 선택 될 때까지 제출 단추를 사용하지 않으려합니다.필드 유효성 검사시 이상한 jQuery 문제

를 열고이 jsfiddle과 순서로 다음을 수행하십시오 Description 분야에서

  1. 쓰기 뭔가.
  2. category을 선택하십시오.

Title 필드가 비어 있으면 제출 버튼이 활성화됩니다. 모두 필드가 채워질 때까지 제출 단추가 비활성화 된 상태로 유지되도록 코드를 변경하려면 어떻게해야합니까?

jQuery 코드 : 당신은 타이틀 입력의 emptyness을 선택해야합니다

jQuery("input[type='text'], textarea").on("keyup", function(){ 
    if(jQuery(this).val() != "" && jQuery("textarea").val() != "" && jQuery("input[name='category']").is(":checked") == true){ 
     jQuery("#subnewtopic").removeAttr("disabled"); 
    } else { 
     jQuery("#subnewtopic").attr("disabled", "disabled"); 
    } 
}); 

jQuery("input[name='category']").on("change", function(){ 
    if(jQuery(this).val() != "" && jQuery("textarea").val() != "" && jQuery("input[name='category']").is(":checked") == true){ 
     jQuery("#subnewtopic").removeAttr("disabled"); 
    } else { 
     jQuery("#subnewtopic").attr("disabled", "disabled"); 
    } 
}); 
+0

나는이 질문의 복사본이 아닙니다. http://stackoverflow.com/questions/27829051/enable-submit-button-only-when-all-fields-are-filled with already many 예 : http://jsfiddle.net/jsq7m8hL/2/ 또는 원하는 경우 http://jsfiddle.net/soyn0xag/40/ –

답변

1

: 당신이 있는지 확인하지 않기 때문에

jQuery("input[type='text'], textarea").on("keyup", function() { 
 
    if (jQuery(this).val() != "" && jQuery("textarea").val() != "" && jQuery("input[name='category']").is(":checked") == true) { 
 
     if ($("#title").val() != '') 
 
     { 
 
      jQuery("#subnewtopic").removeAttr("disabled"); 
 
     } 
 
    } else { 
 
     jQuery("#subnewtopic").attr("disabled", "disabled"); 
 
    } 
 
}); 
 

 
jQuery("input[name='category']").on("change", function() { 
 
    if (jQuery(this).val() != "" && jQuery("textarea").val() != "" && jQuery("input[name='category']").is(":checked") == true) { 
 
     if ($("#title").val() != '') 
 
     { 
 
      jQuery("#subnewtopic").removeAttr("disabled"); 
 
     } 
 
    } else { 
 
     jQuery("#subnewtopic").attr("disabled", "disabled"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form action="" method="post" id="subnewtopicform" />Title: 
 
<input id="title" type="text" name="title" /> 
 
<br/>Description: 
 
<textarea name="description"></textarea> 
 
<br/>Category: 
 
<ul class="list:category categorychecklist form-no-clear" id="categorychecklist"> 
 
    <li id="category-19"> 
 
     <label class="selectit"> 
 
      <input type="radio" id="in-category-19" name="category" value="19">Animation</label> 
 
    </li> 
 
    <li id="category-20"> 
 
     <label class="selectit"> 
 
      <input type="radio" id="in-category-20" name="category" value="20">Anime</label> 
 
    </li> 
 
</ul> 
 
<input type="submit" value="Submit Topic" class="button-primary" name="subnewtopic" id="subnewtopic" disabled="disabled" /> 
 
</form>

Fiddle here

+0

아주 좋습니다. 고맙습니다. –

0

의 그 제목은 em이다. pty 또는 아닙니다. 모두 if들에

jQuery("input[type='text']").val() != "" 

를 추가하고 예상대로 작동합니다.

+0

if 문에이 코드 줄을 추가하여 답변을 업데이트 할 수 있습니까? –

관련 문제