2013-04-15 3 views
0

이 작업이 대부분 있습니다. 선택한 라디오 버튼에 따라 표시된 div를 전환하려고하는데이 방법이 효과적입니다. 또한 숨겨진 입력 (name = martygeocoderlatlngonlyly)의 disabled 속성을 표시 여부에 따라 전환하고 있습니다.jquery로 읽기 전용 및 비활성 전환 토글

그러나 처음에 볼 수있는 입력 (name = martygeocoderlatlngonly)을 읽기 전용에서 사용 안 함으로 전환하고 초점이 맞지 않게 다시 전환하는 데 어려움을 겪고 있습니다.

HTML :

<p>Modify: 
    <label> 
     <input id="rdb1" type="radio" name="toggler" value="1" checked />Location</label> 
    <label> 
     <input id="rdb2" type="radio" name="toggler" value="2" />Lat/Lng</label> 
</p> 
<div id="blk-1" class="toHide" style=""> 
    <p> 
     <label for="martygeocoderaddress">Address</label> 
     <br /> 
     <input class="widefat" type="text" name="martygeocoderaddress" id="martygeocoderaddress" value="" size="30" /> 
    </p> 
    <p> 
     <label for="martygeocoderlatlng">Lat/Lng</label> 
     <br /> 
     <input class="widefat" type="text" name="martygeocoderlatlng" id="martygeocoderlatlng" value="" size="30" readonly /> 
    </p> 
</div> 
<div id="blk-2" class="toHide" style="display:none;"> 
    <p> 
     <label for="martygeocoderlatlng">Lat/Lng</label> 
     <br /> 
     <input class="widefat" type="text" name="martygeocoderlatlngonly" id="martygeocoderlatlng" value="" size="30" disabled/> 
    </p> 
</div> 

JQUERY : 내가 PHP를 사용하여 개발하는 데 사용 및 JQuery와 및 자바 스크립트 나에게 조금 미치게 있도록 OOP에있어 결코

// Toggle display of fields 
$("[name=toggler]").click(function() { 
    //$('.toHide').hide(); 
    $("#blk-" + $(this).val()).each(function() { 
     //$(this).show(); 
    }); 
    $(":input[name='martygeocoderlatlngonly']").each(function() { 
     this.disabled = !this.disabled; 
    }); 
    $(":input[name='martygeocoderlatlng']").each(function() { 
     if (this.readOnly = true) { 
      this.readOnly = false; 
      this.disabled = true; 
     } else { 
      this.readOnly = true; 
      this.disabled = false; 
     } 
    }); 
}); 

.

#

솔루션 :

$("[name=toggler]").click(function() { 
    $('.toHide').hide(); 
    $("#blk-" + $(this).val()).each(function() { 
     $(this).show(); 
    }); 
    $(":input[name='martygeocoderlatlngonly']").each(function() { 
     this.disabled = !this.disabled; 
    }); 
    $(":input[name='martygeocoderlatlng']").each(function() { 
     this.readOnly = !this.readOnly; 
     this.disabled = !this.disabled; 
    }); 
}); 

답변

0
if (this.readOnly = true) { 

if (this.readOnly == true) { 

일반적인 실수 :

+0

감사를 보정 : 당신은 그것을 비교하려는, 그래서 두 개의 등호 기호 사용

if (this.readOnly == true) 

또는 완전히 오른쪽을 생략합니다. 라디오 버튼이 "위치"에서 "위도/경도"로 토글되면 초점이 맞지 않는 입력 (martygeocoderlatlng)이 비활성화됩니다.하지만 다시 스위치를 켜면 비활성화 된 상태로 유지됩니다. – brandonhowlett

0

this.readOnly = true 세트 truethis.readOnly해야한다. 그러나 나는 아직도 어려움을 겪고있어,

if (this.readOnly) 
+0

오, 고마워요. 이 작업을 수행하는보다 효율적인 방법이 있습니까? 나는 jquery가 짧고 간결하다는 것에 대한 모든 것이 있기 때문에 반드시 있어야한다고 생각합니다. – brandonhowlett