簡體   English   中英

下拉以使用Jquery設置初始化值

[英]Drop Down to Set Initialize Value using Jquery

我嘗試使用下拉菜單在所有字段上設置默認值。 例如: Let consider Drop down there is of three options like theme1, theme2 and theme 3,我有5 input fields. five input fields具有適當的id

如果我選擇theme1它將值初始化為適當的5輸入字段。(我不確定是否可以將值分配給數組)

主題1繼續

input1 => "hi";
input2 => "how";
input3 =>"are";
input4 =>"you";

如果我在下拉列表中選擇theme1,則添加我在array中定義的值(如果可行)Theme2 contatin

input1 => "glad";
input2 => "to";
input3 =>"meet";
input4 =>"you";

如果我選擇theme2它將值初始化為適當的5輸入字段。(此值與theme1不同)

$('select').on('change', function() {
    $("select option:selected").each(function () {
            $('#price').val('$' + $(this).attr('value'));
    });

});

現在我很有價值。 但是有一種方法可以調用該函數,而不是有人可以嗎?建議我使用數組將一組初始化值設置為一個選項(theme1)。 這是我的小提琴

工作演示

嘗試這個

$('select').on('change', function () {
    var data = $(this).val().split(' ');
    var i = 0;
    $('input').each(function () {

        $(this).val(data[i]);
        i++;

    });
});

HTML

<select>
    <option>Select an item</option>
    <option value="Hello how are you">Item 1</option>
    <option value="Glad to meet you">Item 2</option>
    <option value="3">Item 3</option>
</select>
<input type="text" id="price">
<input type="text" id="another_price">
<input type="text">
<input type="text">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM