2013-07-12 8 views
1

버튼이 생성 되었으면 어쨌든 다음과 같이 링크를 추가하거나 window.location 메소드를 사용할 수 있습니까 :`window.location = 'nextpage.html? foo = number'. 나는 현재이어떻게 동적으로 생성 된 버튼에 링크를 추가 할 수 있습니까?

var typeValue = location.search; 
var typeStringValue= typeValue.replace("?type=",""); 
var containers = typeValue.replace("?type=multi",""); 
var containersValue = parseInt(containers); 
var sampleLetter = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; 

function createButton(buttonName){ 
    var buttonDivBlock = document.getElementById("sampleSets"); 
    var buttonElement = document.createElement("input"); 
     buttonElement.setAttribute("type","button"); 
     buttonElement.setAttribute("name",buttonName); 
     buttonElement.setAttribute("value","Sample Set"+" "+buttonName); 
     buttonElement.setAttribute("id",buttonName); 
     buttonDivBlock.appendChild(buttonElement); 
    // document.getElementById(sampleLetter[i]).setAttribute('onclick',window.location='SampleInfo.html'+typeStringValue+bottonName);<!--add the button link --> 
} 

function setButtons(numberOfContainers){ 

    for(i=0;i<numberOfContainers;i++){ 
     createButton(sampleLetter[i]); 

    } 
} 

window.onload = function(){ 
    setButtons(containersValue); 
} 

그러나 document.getElementById("'"+sampleLetter[i]+"'").setAttribute('onclick',window.location='SampleInfo.html'+typeStringValue+bottonName);<!--add the button link --> 반환 널 값을 가지고있다.

+0

가능한 속는/관련 질문을 추가하지 마십시오, 루프 대신의 createButton 기능에서 설정하려는 경우 : http://stackoverflow.com/questions/789824/add-onclick-property-to-input-javascript, http://stackoverflow.com/questions/6909988/add-an-onclick-event-to-a-div,http://stackoverflow .com/questions/3316207/add-onclick-event-to-newly-added-element-in-javascript. . . "onclick"은 유효한 속성이 아니므로 샘플 코드가 시도하는 것입니다. – ernie

+0

글쎄 그게 아니라 성공을 시도했기 때문에 내가 뭘 찾고 있어요. : – Ch32k0

+0

@ernie 다른 페이지에서도 여전히 사용했습니다. – Ch32k0

답변

1

글쎄, 어쩌면 내가 예와 함께 당신을 도울 수 있습니다 : callOtherFunction의 두 번째 인수는 5가 아닌 getFive 기능이 될 것입니다

function getFive() { return 5;} 

callOtherFunction("stringArgument", getFive()); 

. 대부분의 경우 이벤트 리스너 및 AJAX 코드를 추가하는 것과 같이 실제로 함수 자체를 인수로 전달하므로 나중에 호출 할 수 있습니다. 당신이 별도로 그 함수를 선언 귀찮게하지 않으려는 경우, 그것은 다음과 같습니다

callOtherFunction("stringArgument", function() { return 5; }); 

, 당신 Enter 키를 누를 수있는 코드를 깔끔하게 할하려면 {및 확인 멀티 라인 기능 후.

이제 모든 것을 염두에두고, 주석 처리 한 행을 다시 한 번 살펴보십시오. 누락 된 것이 보이십니까? (추신 : "egging-on"포맷에 대한 사과 - 해결책을 찾는데 도움이된다면 솔루션을 찾는데 도움이된다면 사람들이 더 잘 이해하게됩니다.

+0

시도해 주셔서 감사합니다! – Ch32k0

+0

WORKED !! 정말 고맙습니다! – Ch32k0

+0

나는 그것을 조금 비틀었다;) ... 나의 대답을 게시하고 당신에게 신용을 줄 것이다! ;) – Ch32k0

0

sampleLetter 변수는 정의되지 않았습니다. 당신은 그것을 사용하려고합니다 (주석 처리 된 코드로 판단). 몇 줄 앞의 id 속성에 방금 설정 한 값을 사용하십시오.

document.getElementById(buttonName).setAttribute(/* ... */); 

아니면 작은 따옴표를

document.getElementById(sampleLetter[i]).setAttribute(/* ... */); 
+0

죄송합니다. 코드를 붙여 넣는 동안 죄송합니다 ... 눈치 채어 주셔서 감사합니다;) – Ch32k0

+0

..성공하지 못했습니다 : – Ch32k0

+0

'document.getElementById (sampleLetter [i])'또는'document.getElementById (buttonname)'이 여전히 null이라는 의미는 아닙니다. 또한 코드 작동을 방해하는 추가 문제가있을 수 있습니다. 그 대답을 찾는 첫 번째 단계는'getElementById'를 호출하여 null이 아닌 요소를 반환하는지 확인하는 것입니다. –

관련 문제