简体   繁体   English

根据Jquery中的下拉选择框填充输入文本框

[英]Populate input text box based on drop down select box in Jquery

I have a drop down select box and input text box. 我有一个下拉选择框和输入文本框。 Select box display my categories and its look like this: 选择框显示我的类别,它看起来像这样:

<select id="category" name="category"> 
  <option value="">Please select...</option> 
  <option value="1">Category-1</option> 
  <option value="2">Category-2</option> 
  <option value="3">Category-3</option>   
  <option value="4">Other</option> 
</select>

Input text box is like this: 输入文本框是这样的:

<input type="text" id="otherCategory" name="otherCategory" value="" style="display: none;">

My question is. 我的问题是。 when an user select only "Other" from dropdown then I need to populate the input text. 当用户从下拉列表中仅选择“其他”时,我需要填充输入文本。

I tried it something like this: 我试过这样的事情:

$(document).ready(function() {

    $('#category').change(function() {
        var myValue = $(this).val();
        var myText = $("#category :selected").text();

        if (myText != '' AND myText == "Other") {
           $("#otherCategory").show();
        }
    });
});

But I couldn't get it to work. 但我无法让它发挥作用。 Can anybody tell how I figure this out. 任何人都可以告诉我如何解决这个问题。

NOTE: my dropdown select populating dynamically. 注意:我的下拉列表选择动态填充。

Thank you. 谢谢。

You are missing && in if condition. 你是否缺少&& in if条件。 Also, your condition 还有,你的病情

myText != '' is redundant and not required. myText != ''是多余的,不是必需的。

And you need to hide the input when selection changed. 并且您需要在选择更改时隐藏input

$(document).ready(function () {

    $('#category').on('change', function () {
        var myValue = $(this).val();
        var myText = $.trim($("#category :selected").text()).toLowerCase(); // Trim spaces and convert to lowercase for comparison

        $("#otherCategory").toggle(myText === 'other');
    });
});

Demo: https://jsfiddle.net/tusharj/8ykfmtyt/1/ 演示: https//jsfiddle.net/tusharj/8ykfmtyt/1/

You need to use && instead of AND 您需要使用&&而不是AND

Live Demo 现场演示

if (myText != '' && myText === "Other") {
    $("#otherCategory").show();
}

You can further optimize it by hiding with option other then 'other' is selcted. 您可以通过隐藏选项以进一步优化它,然后选择“其他”。 You do not need to check if it is not empty when you are comparing it with string 'other' so I removed that condition from if statement. 当你将它与字符串'other'进行比较时,你不需要检查它是否为空,所以我从if语句中删除了这个条件。

Live Demo 现场演示

$('#category').change(function () {
      $(this).find(":selected").text() === "Other" ?  
       $("#otherCategory").show() :  $("#otherCategory").hide();
});

Try this Demo , if user selects other option showing input field else hiding. 如果用户选择其他选项显示输入字段,则尝试此演示 ,否则隐藏。

$(document).ready(function() {

    $('#category').change(function() {
        var myValue = $(this).val();
        var myText = $("#category :selected").text();

        if (myText == "Other") {
           $("#otherCategory").show();
        }
        else{
            $("#otherCategory").hide();
        }
    });
});

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

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