简体   繁体   English

选择选项时显示隐藏文本的jQuery

[英]jquery to show hidden text when an option is selected

  <form method="get" action="processform.php">
  <b>SELECT SERVICE</b><select name="Service">
  <option value="">Select a Service</option>
  <option value="toairport">To Airport</option>
  <option value="fromairport">From Airport</option>
  <option value="DriveAround">Drive Around</option>
  <option value="PointToPoint">Point TO Point</option>
  <option value="Wedding">Wedding</option>
  <option value="Prom">Prom</option>
  <option value="Graduation">Graduation</option>
  <option value="Birthday">Birthday</option>
  <option value="Concert">Concert</option>
  <option value="SportingEvents">Sporting Events</option>
  <option value="Anniversary">Anniversary</option>
  </select>

   Airports<select name="airports">
  <option value="">Select an Airport</option>
  <option value="LGA">LaGuardia</option>
  <option value="JFK">Jonh F. Kennedy</option>
  <option value="Westchester">Westchester</option>
  <option value="Terteboro">Terteboro</option>
  <option value="Islip">Islip</option>
  </select>

I have the above code, In the service section when i click "from airport or to airport i wan the Airport option to show. thanks in advance 我有上面的代码,在“服务”部分中,当我单击“从机场或到机场时,我要显示“机场”选项。在此先感谢。

I'd suggest: 我建议:

$('select[name="Service"]').change(function(){
    var v = $(this).val().toLowerCase();
    $('select[name="airports"]').toggle((v == 'toairport' || v == 'fromairport'));
}).change();

JS Fiddle demo . JS小提琴演示

You could also use a simple regular expression to test that the value ends with the word airport (which matches the requirements as posted, in that both options for which you're testing end with the string 'airport'): 您还可以使用一个简单的正则表达式来测试该value以单词airport (符合发布的要求)结尾,因为您要测试的两个选项都以字符串'airport'结尾):

$('select[name="Service"]').change(function(){
    var v = $(this).val().toLowerCase();
    $('select[name="airports"]').toggle(v.match(/airport$/));
}).change();

JS Fiddle demo . JS小提琴演示

References: 参考文献:

Try this 尝试这个

$("select[name=Service]").change(function(e){
    if(e.target.value == "toairport" || e.target.value == "fromairport"){
        $("select[name=airports]").show();
    }else{
        $("select[name=airports]").hide();
    }
})

You might be looking for this: 您可能正在寻找:

$("#airports").hide()
$("#service").change(function(){
    if(this.value == 'toairport' || this.value == 'fromairport')
       $("#airports").show();
    else
        $("#airports").hide();
});

Markup: 标记:

<select name="Service" id="service">
<select name="airports" id="airports">

DEMO --> http://jsfiddle.net/jA56n/ 演示 -> http://jsfiddle.net/jA56n/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM