0

次のようなラジオボタンとドロップダウンがあります。

落ちる

 <asp:RadioButtonList ID="rblTravelType" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" EnableViewState="false" onclick="TypeOfTravelOnChange('rblTypeOfTravel');">
        <asp:ListItem Value="A" Text="Air Travel"></asp:ListItem>
            <asp:ListItem Value="O" Text="Other Travel"></asp:ListItem>
    </asp:RadioButtonList>

ラジオボタン

<select name="DrTravelMode" id="DrTravelMode">
    <option value="">--Select--</option>
    <option value="A">Air</option>
    <option value="T">Bus</option>
    <option value="B">train</option>
<select>

脚本 :

function TypeOfTravelOnChange(RadioButtonId) {
if ($('input:radio[name=' + RadioButtonId + ']:checked').val() == 'O') {
 $("#DrTravelMode option[value='A']").remove(); 
}
else
{
 // here i want to append the dropdown value "Air" between --Select-- and Bus
}

}

私の質問は、 JavaScriptを使用して2つのアイテムの間に新しいリストアイテムを挿入する方法です..

4

2 に答える 2

0

以下のスクリプトをelseブロックに入れましたが、うまくいきます..

var select = document.getElementById("DrTravelMode");
    if ($("#DrTravelMode option[value='A']").length == 0) {
        var opt2 = document.createElement("option");
        opt2.text = "Air";
        opt2.value = "A";
        select.add(opt2, 1);
    }
于 2013-10-23T09:22:58.683 に答える
0

このように使用できます

インデックスを見つけて、次のように使用する必要があります

$("select option").eq(index).before($("<option></option>").val(val).html(text));

あなたの場合

次のように最初にインデックスを見つけます

var index= $("#DrTravelMode option[value='A']").index()

次に、削除してから上記のインデックスに追加します

于 2013-10-23T08:33:59.140 に答える