Javascript Jquery - 选择选项时填充输入文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6405787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:34:38  来源:igfitidea点击:

Jquery - Fill an input text when select option

javascriptjqueryarraysforms

提问by Carlos

I have this form's elements:

我有这个表单的元素:

<form ...>
    <select name="type[]" id="type[]">
        <option value="1">One</option>
        <option value="2">Two</option>
    </select>
    <input type="text" name="tvalue[]" id="tvalue[]" />

    <select name="type[]" id="type[]">
        <option value="1">One</option>
        <option value="2">Two</option>
    </select>
    <input type="text" name="tvalue[]" id="tvalue[]" />
    ...

    <input type="submit" value="send" />
</form>

so, I need fill the input text element when select option element change. (array of elements?) can anybody help me, please?

因此,选择选项元素更改时,我需要填充输入文本元素。(元素数组?)有人可以帮我吗?

The markup above is result of this PHP script (I changed the markup above):

上面的标记是这个 PHP 脚本的结果(我更改了上面的标记):

<?php
echo "<form name='form1' id='form1' action=\"#\" method='post'>";
for ($i=0; $i<5; $i++){
echo "<select name='type[]' id='type[]'>\n";
echo "<option value ='1'>One</option>\n";
echo "<option value ='2'>Two</option>\n";
echo "</select>\n";
echo "<input type='text' name='tvalue[]' id='tvalue[]' value=''>";
}
echo "<input type='submit' name='submit' id='submit' value='enviar' />";
echo "</form>";
?>

(type[] and tvalue[] because of I ned post this an array)

(type[] 和 tvalue[] 因为我 ned 发布了这个数组)

The problem is that I need fill the input text when I select option. For example, when I select option "One" in the first select, then the first input text must show "One", and so when I select an option in the second select, and the third, and so... So when I submit the form I can get this values.

问题是我选择选项时需要填写输入文本。例如,当我在第一个选择中选择选项“一个”时,第一个输入文本必须显示“一个”,所以当我在第二个选择中选择一个选项时,第三个等等......所以当我提交表单我可以获得这个值。

Thanks you very much! Carlos.

非常感谢你!卡洛斯。

回答by karim79

1 - Remove dupe IDs they are not valid.

1 - 删除无效的欺骗 ID 。

2 - Assuming your markup is exactly what you posted, give or take more selects and corresponding textboxes, and no elements in between:

2 - 假设您的标记正是您发布的内容,给予或接受更多选择和相应的文本框,并且中间没有元素:

$("select[name='type[]']").change(function() {
    $("input[name='tvalue[]']").eq($(this).index()).val(this.value);
}).change(); // set initial textbox value on page load

Demo: http://jsfiddle.net/NPeJL/2/

演示:http: //jsfiddle.net/NPeJL/2/

回答by Juan Sebastian Totero

I write down the example in the case of only one select and one input to modify

我把例子写在只有一选一输入的情况下修改

$('select#id1').change(function(){ //the event here is change
    if ($(this).val() === "1" ) //check the value into the select
    {
        $('input#id2').val("ONE!"); //change the value into the input
    }
    else
    {
        $('input#id2').val("TWO!"); //change the value into the input
    }
});