1

我尝试使用下拉菜单在所有字段上设置默认值。例如: Let consider Drop down there is of three options like theme1, theme2 and theme 3 and I have 5 input fields.in the five input fieldshave 适当的id

如果我选择theme1它会将值初始化为适当的5输入字段。(我不确定是否有办法将值分配给数组)

主题1包含

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

如果我在下拉列表中选择主题 1,则添加我在数组中定义的值(如果可行)主题 2 包含

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

如果我选择theme2它会将值初始化为适当的5输入字段。(此值与主题1不同)

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

});

现在我提到了价值。但是有没有一种方法可以调用函数而不是任何人都可以建议我使用数组的一个选项(主题1)的一组初始化值来执行此操作。这是我的小提琴

4

1 回答 1

1

工作演示

尝试这个

代码

$('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">
于 2013-09-26T08:57:48.123 回答