简体   繁体   English

使用下拉菜单选项创建文本字段的 Javascript

[英]Javascript to create text field with dropdown menu options

I have been using some Javascript to create a text field once an certain value is selected from a drop-down box, but having a limited knowledge in Javascript now that I need to edit it so that a new dropdown option also creates a text field.一旦从下拉框中选择了某个值,我一直在使用一些 Javascript 来创建文本字段,但是现在我对 Javascript 的了解有限,我需要编辑它,以便新的下拉选项也创建一个文本字段。 When I have tried to edit it seems not not work.当我尝试编辑时,它似乎不起作用。 Here is the js I'm an trying to use:这是我尝试使用的 js:

$(".claim").change(function() {
    $(".area").find(".field").remove();
    //or
    $('.area').remove('.field');
    if ($(this).val() == "Insurance") {
        $(".area").append("<input class='field' name='cost' type='text' placeholder='Cost' />");

    }
    if ($(this).val() == "Damage &lt;$100") {
        $(".area").append("<input class='field' name='cost' type='text' placeholder='Cost' />");

    }
});

and here is the html dropdown box:这是 html 下拉框:

<div id="area" class="area">

    <strong>Cases:</strong><input name="cases" type=text  placeholder="Cases ID" maxlength="7" style="width:129px;">
    <br />
    <strong>Claim:</strong>
    <select class="claim" id="claim" name="claim"> 
    <option value="">Select a Claim</option>
    <option value="Insurance">Insurance</option>  
    <option value="Warrenty">Warrenty</option>
    <option value="Damage &lt;$100">Damage &lt;$100</option>
    </select>

I have attempted to edit the Javascript and a few other things but none seem to work.我曾尝试编辑 Javascript 和其他一些东西,但似乎都不起作用。 I am guessing its something that is simple but I am unsure about using Javascript at this moment so any help would be great.我猜它很简单,但我现在不确定是否使用 Javascript,所以任何帮助都会很好。

Use采用

$(this).val() == "Damage <$100"

instead of代替

$(this).val() == "Damage &lt;$100"

DEMO演示

Note笔记

You're comparing string so not need any special character.您正在比较字符串,因此不需要任何特殊字符。

Some more多一点

Instead of $(this).val() use this.value , would be faster.而不是$(this).val()使用this.value ,会更快。

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

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