最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Populate input text box based on drop down select box in Jquery - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked May 15, 2015 at 6:39 user3733831user3733831 2,92610 gold badges39 silver badges73 bronze badges 1
  • 1 Replace your AND with && – stanze Commented May 15, 2015 at 6:42
Add a ment  | 

3 Answers 3

Reset to default 6

You are missing && in if condition. Also, your condition

myText != '' is redundant and not required.

And you need to hide the input when selection changed.

$(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 parison

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

Demo: https://jsfiddle/tusharj/8ykfmtyt/1/

You need to use && instead of 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 paring it with string 'other' so I removed that condition from if statement.

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();
        }
    });
});
发布评论

评论列表(0)

  1. 暂无评论