2015-06-27 35 views
1

我需要使用變量的值來顯示文本框。對我而言,我無法完成它的工作。我不是java腳本的專家,所以我正在尋求你的幫助。下面是HTML和Java腳本:使用變量顯示文本框

function valueChanged(me) 
 

 
{ 
 
\t var check_id = me.id + "dt" 
 
\t alert (check_id) 
 
\t 
 
    if($('.tour_selected').is(":checked")) 
 
\t \t $(check_id).val.show(); 
 
    else 
 
     $(check_id).val.hide(); 
 
}
<input class="t1dt" type="text" name="keywords_other_option" value="" id="t1dt" placeholder=" Select date+time for>" style="display: none"> 
 

 
<input class="tour_selected col-md-1" type="checkbox" name="ATV and Falls " value="ATV and Falls" id="t1" onchange=" return valueChanged(this)">ATV and Falls 
 
<br>

正如你所看到的ID是被串聯和顯示。但是沒有傳入IF函數來取消隱藏文本框。

感謝您的幫助

+0

是要使用jQuery庫或JavaScript? –

回答

1

你忘了包括jQuery的,要指定您需要的井號的ID,你只是做顯示/隱藏不val.show/hide。

function valueChanged(me) 
 

 
{ 
 
\t var check_id = me.id + "dt" 
 
\t alert (check_id) 
 
\t 
 
    if($('.tour_selected').is(":checked")) 
 
\t \t $('#'+check_id).show(); 
 
    else 
 
     $('#'+check_id).hide(); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
 

 
<input class="t1dt" type="text" name="keywords_other_option" value="" id="t1dt" placeholder=" Select date+time for>" style="display: none"> 
 

 
<input class="tour_selected col-md-1" type="checkbox" name="ATV and Falls " value="ATV and Falls" id="t1" onchange=" return valueChanged(this)">ATV and Falls 
 
<br>

+0

這個工作完美,我會測試它與多個複選框和文本框和建議。 –

+0

這適用於多個複選框和文本框。謝謝你的depperm! –

0
我已經使用JavaScript來此,而不是jQuery庫

JS

function valueChanged() { 

       var checkBoxElement = document.getElementById('tour_selected_id'); 
       var textBoxElement = document.getElementById('t1dt'); 

       console.log(checkBoxElement.style.visibility) 
       if(checkBoxElement.checked) { 
        textBoxElement.style.display = 'block'; 
       } 
       else{ 
        textBoxElement.style.display = 'none'; 
       } 

      } 

HTML

<input class="t1dt" id="t1dt" type="text" name="keywords_other_option" value="showhide" id="t1dt" placeholder=Select date+time for" style="display: none"> 

<input class="tour_selected col-md-1" id="tour_selected_id" type="checkbox" name="ATV and Falls " value="ATV and Falls" id="t1" 
onchange=" valueChanged()">ATV and Falls 
<br> 
+0

謝謝RIYAJ KHAN。但該框的ID將根據複選框選中而改變:t1dt,t2dt,t3dt等。 –

+0

請您分享一下這個示例html嗎? –