2011-12-10 3 views
0

내가 여러 입력을 가지고 가정,현재 활성 입력 옆에있는 입력에 액세스하는 방법은 무엇입니까?

<input type="text" name=ABC onkeyup='MyFunction(this)'> 
<input type="text" name=ABC> 
<input type="text" name=ABC> 


function MyFunction(activeInput) { 
    /*... some code here to create array 'thisArray', of length 3 whose values are to be assigned to the 3 inputs. ...*/ 
    activeInput.value = thisArray[0]; 
    /*... this is where i need help. How do i assign the other 2 values to other 2 inputs...*/ 
} 

이러한 입력에는 ID를 가지고,하지만 같은 이름을 가지고있다. JQuery도없고 Javascript 만 있습니다.

미리 감사드립니다.

+0

당신은 당신이 달성하려고하는 것에 eloborate 수 있을까요? 당신이 쓰려고하는 코드는 정말로 뒤로 보이고 당신의 문제에 대한 더 똑똑한 해결책이있을 것입니다. –

+0

나는 이미 존재하는 코드에 functionailty를 추가하려고 시도하고 있으며, 이것을 더 이상 설명 할 수 없습니다. getElementsByName이 트릭을했습니다. UR 시간을 가져 주셔서 감사합니다! – trinity

+0

var siblings = Array.prototype.slice.call (activeInput.parentNode.getElementsByTagName ('input'))); – benastan

답변

2
//not tested 
<input type="text" name=ABC onkeyup='MyFunction(this,0)'> 
<input type="text" name=ABC onkeyup='MyFunction(this,1)'> 
<input type="text" name=ABC onkeyup='MyFunction(this,2)'> 


function MyFunction(activeInput,position) { 
    activeInput.value = thisArray[position]; 
} 

할당과 ID가 더 쉬울 것이라고 생각합니다.

activeInput.value = thisArray[activeInput.id]; 
0
var inputs = document.getElementsByName("ABC"); 
for (i = 0; i < thisArray.length; i++) { 
    inputs[i].value = thisArray[i]; 
} 
+0

입력 [i]가 정의되어 있는지 조심하십시오! – yoshi

+0

예, 확인하지 않으려면이를 추가하십시오. – trinity

관련 문제