2014-03-07 108 views
2

一个文本框页面加载时我想input textbox要隐藏的ID为:AMF-输入othertitle_19jQuery的 - 如何显示基于值从下拉列表

但是,如果用户挑选从上面的文本框的下拉列表中选择一个确定的值(“其他”),以便他们可以填写该框中的信息。

下面的代码下拉列表:

<select name="title_3" id="amf-input-title_3"> 
    <option value="Title" id="title_3-0">Title</option> 
    <option value="Mr" id="title_3-1">Mr</option> 
    <option value="Mrs" id="title_3-2">Mrs</option> 
    <option value="Miss" id="title_3-3">Miss</option> 
    <option value="Ms" id="title_3-4">Ms</option> 
    <option value="Dr" id="title_3-5">Dr</option> 
    <option value="Professor" id="title_3-6">Professor</option> 
    <option value="Other" id="title_3-7">Other</option> 

文本框隐藏/显示:

<input type="text" class="text" 
    name="othertitle_19" id="amf-input-othertitle_19" 
    value="" placeholder="Please specify other...." 
    maxlength="255" 
    onkeyup="if (this.length>255) this.value=this.value.substr(0, 255)" 
    onblur="this.value=this.value.substr(0, 255)" 
/> 

回答

0
$(document).ready(function(){ 
    $('#amf-input-othertitle_19').hide(); //Hide on PageLoad 

    $('#amf-input-title_3').change(function(){ 
     if($(this).val() == 'Other'){ 
     $('#amf-input-othertitle_19').show(); //show if dropdown = other 
     } 
     else{ 
     $('#amf-input-othertitle_19').hide(); //hide if changed to something else 
     } 

    }); 
}); 
+0

谢谢戴克澜 – user3120015

0

.change()

var inp = $('#amf-input-othertitle_19'); 
$('#mf-input-title_3').change(function() { 
    if (this.value == 'other') { //check selected value 
     inp.show(); 
    } else { 
     inp.hide(); 
    } 
}); 
0

您需要观看select的ID,并显示它当它被选中时

$('document').ready(function() { 
    $('#amf-input-title_3').change(function() { 
     if($(this).val() == 'Other') { 
      $('#amf-input-othertitle_19').fadeIn(); 
     } 
     else{ 
      $('#amf-input-othertitle_19').fadeOut(); 
     } 
    }) 
}); 
0

的JavaScript:

// Shorthand for $(document).ready(function() { ... }); 
$(function() { 

    // It's more efficient to save off the jQuery object 
    // than to re-traverse the DOM each time. 
    var $input = $('#amf-input-othertitle_19'); 

    // Catch the change on <select /> change 
    $('#amf-input-title_3').on('change', function() { 

     // Quick little ternary operator... 
     (this.value === "other") ? $input.show() : $input.hide(); 
    }); 
}); 

CSS:

// Make sure to hide the input on initial load... 
#amf-input-othertitle_19 { 
    display: none; 
} 

JSFiddle

相关问题