使用变量显示文本框

时间:2015-06-27 18:43:26

标签: javascript

我需要使用变量的值来显示文本框。对于我的生活,我无法让它发挥作用。我不是java脚本的专家所以我正在寻求你的帮助。下面是html和java脚本:

function valueChanged(me)

{
	var check_id = me.id + "dt"
	alert (check_id)
	
    if($('.tour_selected').is(":checked")) 
		  $(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函数来取消隐藏/隐藏文本框。

感谢您的协助

2 个答案:

答案 0 :(得分:1)

你忘了包含jquery,指定你需要英镑符号的id,你只是显示/隐藏val.show/hide。

&#13;
&#13;
function valueChanged(me)

{
	var check_id = me.id + "dt"
	alert (check_id)
	
    if($('.tour_selected').is(":checked")) 
		  $('#'+check_id).show();
    else
        $('#'+check_id).hide();
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分: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>