2013-10-04 2 views
0
<input type="text" name="fruits[]" value="Apple"> 
<input type="text" name="fruits[]" value="Banana"> 
<input type="text" name="fruits[]" value="Orange"> 

제 질문은 jquery를 사용하여 "Orange"값을 "Grapes"로 변경하는 방법입니다. 아래 코드가 작동하지 않습니다.jQuery를 사용하여 배열 필드의 값을 변경하십시오.

<script> 
    $("input[name='fruits[2]']").val("Grapes"); 
</script> 

미리 감사드립니다.

답변

1

eq(index) 사용해보십시오 : 코드가 필요

$(function(){ //<-- Add this in DOM ready wrapper as well 
    $("input[name='fruits[]']").eq(2).val("Grapes"); 
}); 

Fiddle

1
  1. 가 DOM 준비 처리기에서 실행되는 당신의 선택 input[name='fruits[2]'] 이름 fruits[2]와 입력 요소 대신에 보이는
  2. 이름이 fruits[]이고 세 번째 색인에 있습니다

그래서

jQuery(function() { 
    $("input[name='fruits[]']:eq(2)").val("Grapes"); 
}) 

데모 : Fiddle

0

이 시도 :

$("input[value='Orange']").val('Grapes'); 
관련 문제