2017-11-24 8 views
0

내 HTML 코드 : 사용자 선택 이유 4, 그것은 텍스트 영역 태그select에서 이름을 제거하고 javascript 또는 jquery로 textarea에 이름을 추가하는 방법은 무엇입니까? 이 같은

에 이름 = "이유를"select 태그에 이름 = "이유"를 제거하고 추가 할 경우

$('input[name="is_follow_up"]').change(function(){ 
 
     var val = $(this).val(), 
 
      input = $('.input-reason'); 
 
     if (val == 'n') 
 
      input.show() 
 
     else 
 
      input.hide(); 
 
    }); 
 
    $('.select-reason').change(function(){ 
 
     var reason = $(this).val(), 
 
      others = $('.textarea-others'); 
 
     if (reason == 4) 
 
      others.show(); 
 
     else 
 
      others.hide() 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label class="radio-inline"> 
 
     <input type="radio" name="is_follow_up" id="inlineRadio1" value="y" required checked> accept 
 
    </label> 
 
    <label class="radio-inline"> 
 
     <input type="radio" name="is_follow_up" id="inlineRadio2" value="n" required> reject 
 
    </label> 
 
    <div class="row input-reason" style="display: none"> 
 
     <div class="col-sm-2">Reason:</div> 
 
     <div class="col-sm-5"> 
 
      <div class="form-group"> 
 
       <select class="form-control select-reason" name="reason"> 
 
        <option value="Reason 1">Reason 1</option> 
 
        <option value="Reason 2">Reason 2</option> 
 
        <option value="Reason 3">Reason 3</option> 
 
        <option value="4">Reason 4</option> 
 
       </select> 
 
      </div> 
 
      <div class="form-group textarea-others" style="display: none"> 
 
      <textarea rows="5" class="form-control"></textarea> 
 
      </div> 
 
     </div> 
 
    </div>
내가 원하는

어떻게하면됩니까? 당신이 그렇게 할 것입니다 왜

+1

을 추가 할 jQuery를 사용할 수 있습니까? 최종 목표는 무엇입니까? – Nisarg

답변

1

그럼 당신은 단순히/제거 이름 속성

$('.select-reason').change(function(){ 
    var reason = $(this).val(), 
     others = $('.textarea-others'); 
    if (reason === '4') { 
     others.show(); 
     $(this).removeAttr('name'); 
     others.find('textarea').attr('name', 'reason'); 
    } else { 
     others.hide(); 
    } 
}); 
+0

'=='와'==='의 차이점은 무엇입니까? –

+1

1.) ==는 데이터 형식에 대해 고려하지 않는 값 (예 : 4 == "4"또는 4 == 4) 만 확인합니다 (예 : 둘 다 true를 반환합니다). 2) === 데이터 유형을 검사합니다 (예 : 4 === 4는 true를 반환하지만 4 === "4"는 false를 반환합니다) –