开发者

Jquery selects and inputs texts

开发者 https://www.devze.com 2023-03-14 00:14 出处:网络
I have this form: <form ...> <select name=\"fuits[]\" id=\"fruits[]\"> <option value=\"A\">Apple</option>

I have this form:

<form ...>
<select name="fuits[]" id="fruits[]">
    <option value="A">Apple</option>
    <option value="O">Orange</option>
    <option value="P">Pear</option>   
</select>
<input type="text" name="fruit[]" id="fruit[]" />

<select name="fruits[]" id="fruits[]">
    <option value="A">Apple</option>
    <option value="O">Orange</option>
    <option value="P">Pear</option>   
</select>
<input type="text" name="fruit[]" id="fruit[]" />

<select 开发者_运维百科name="fruits[]" id="fruits[]">
    <option value="A">Apple</option>
    <option value="O">Orange</option>
    <option value="P">Pear</option>   
</select>
<input type="text" name="fruit[]" id="fruit[]" />

...

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

So, I need fill each input text from each select option values (match input text with the selected option)

I have this JQuery, but it doesn't work fine =(,

$("select[name='type[]']").change(function() {
    $("input[name='tvalue[]']").eq($(this).index()).val(this.value);
}).change();

Here is the DEMO (thanks to karim79): http://jsfiddle.net/GKrd9/4/ suggestions please?

Thanks! Carlos


I'v edited your fiddle, hope this is what you want.

$("select[name='type[]']").change(function() {
    $(this).next().val(this.value);
}).change();

http://jsfiddle.net/GKrd9/5/


The only thing missing in your code is a selector in index().

This fixes your problem :

$("select[name='type[]']").change(function() {
    $("input[name='tvalue[]']").eq($(this).index("select")).val(this.value);
}).change();

jsFiddle example


Try this:

$(function() {
    $("select").change(function() {
        var jqThis = $(this);
        jqThis
            .next('input')
            .val(jqThis.find('option:selected').val())
    }).change();
});

Here's a jsfiddle: http://jsfiddle.net/HPYxy/

0

精彩评论

暂无评论...
验证码 换一张
取 消