1

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. When I have tried to edit it seems not not work. Here is the js I'm an trying to use:

$(".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:

<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. I am guessing its something that is simple but I am unsure about using Javascript at this moment so any help would be great.

4

1 に答える 1

2

使用する

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

それ以外の

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

デモ

ノート

文字列を比較しているので、特殊文字は必要ありません。

もう少し

$(this).val()を使用する代わりにthis.value、より高速になります。

于 2012-06-08T03:37:09.947 に答える