0

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

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.