2014-04-29 3 views
0

그래도 여전히 매우 새로운 JS입니다. 내가하고있는 것을 100 % 확신하지 못합니다.JavaScript를 사용하여 양식 만들기

자바 스크립트를 사용하여 입력 폼이있는 새 창을 만들고 싶습니다.

하나의 옵션 드롭 다운으로 기본 창을 사용할 수 있었지만 양식 작동 방법을 고민 중입니다.

var i, l, options = [{ 
    value: 'fName', 
    text: 'First Name:' 
}, 
{ 
    value:'lName', 
    text:'Last Name:' 
}, 

{ 
    value: 'age', 
    text: 'Age:' 
}, 
{ 
    value:'city:', 
    text:'City:' 
}, 
{ 
    value:'state', 
    text:'State:' 
}, 

{ 
    value:'zCode', 
    text:'Zip Code:' 
} 
        ], 

newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no"); 

newWindow.document.write("<select onchange='window.opener.setValue(this.value);'>"); 
for(i=0,l=options.length; i<l; i++) { 
    newWindow.document.write("<option value='"+options[i].value+"'>"); 
    newWindow.document.write(options[i].text); 
    newWindow.document.write("</option>"); 
} 

newWindow.document.write("</select>"); 
newWindow.document.title = "Info Window"; 

이 잘 작동하지만 난 형태의 구조에 이상이 전환하는 방법을 알아낼 수 없습니다. 어떤 도움이라도 대단히 감사합니다.

답변

1

원시 HTML을 작성하는 대신 DOM 요소 객체로 작업해야합니다. 다음과 같이 양식을 새 창에 추가 할 수 있습니다.

// create a form and set properties 
var form = document.createElement('form'); 
form.action = 'http://google.com'; 
form.method = 'post'; 

// insert into the body of the new window 
newWindow.document.body.appendChild(form); 

// add text before the input 
var p = document.createElement('p'); 
form.appendChild(document.createTextNode('Enter text:')); 

// add a text input 
var input = document.createElement('input'); 
input.type = 'text'; 
input.name = 'test'; 
input.value = 'this is an input'; 
form.appendChild(input); 
+0

아하는 훨씬 더 깔끔합니다. 2 빠른 질문 1. 입력란 앞에 이름이 표시되지 않습니다. 2. 여러 요소를 추가하는 방법 추가 요소를 만들려고했는데 취하지 않은 것 같습니다. Thanls – user3577459

+0

입력 전에 텍스트를 추가하려면'document.createElement()'와 똑같은 작업을 수행하십시오. 대신 '

'또는 레이블을 작성하십시오. 업데이트 된 답변보기 양식에 여러 입력을 추가해도 문제가없는 경우 코드가 어떻게 생깁니 까? – MrCode

+0

OK 첫 번째 입력 창을 가져올 수 있지만 두 번째 작업을 수행 할 때 createElement 플래그에서 뭔가를 엉망으로 만들고 있다고 생각합니다. – user3577459

관련 문제