2013-05-10 2 views
1

여러 매개 변수를 전달하는 양식이 있습니다. 지금까지 주식 매개 변수가 전달되고있다 : I은이 매개 변수 문자열에 추가로 폼 요소를 추가하려고 해요JavaScript를 통해 양식에 매개 변수 전달

var params = "title=" + document.getElementById("title").value + 
      "&url=" + document.getElementById("url").value + 
      "&snippet=" + document.getElementById("snippet").value + 
      "&tags=" + document.getElementById("tags").value + 
      "&status_bookmark=" + document.getElementById("status_bookmark").value + 
      "&comment=" + document.getElementById("comment").value + 
      "&status_comment=" + document.getElementById("status_comment").value; 

: 여기

var i, lng = document.getElementById('addbookmark').length; 
// If the length property is undefined, then there is only one checkbox. 
if (typeof lng === "undefined") { 
    params + "&topic-link-item-1=" + document.getElementById("topic-link-item-1").value; 
    params + "&topic-link-comment-box-1=" + document.getElementById("topic-link-comment-box-1").value; 
} 
else { 
    for (i = 0; i < lng; i++) { 
     params + "&topic-link-item-" + i + "=" + document.getElementById("topic-link-item-" + i).value; 
     params + "&topic-link-comment-box-" + i + "=" + document.getElementById("topic-link-comment-box-" + i).value; 
    } 
} 

을, 나는 코드에서 가져온 사용했습니다 another StackOverflow article에서 볼 수 있듯이, jQuery를 통해 다른 곳에서 생성하는 임시 양식 요소와 일치하는 일련의 쌍을 이루는 매개 변수를 작성하려고합니다.

그러나 이러한 값은 다른 양식 요소가 전달되는 동안 양식을 통해 전달되지 않는 것처럼 보입니다.

제안 사항?

업데이트 내가 제안에 따라 코드를 수정했습니다

,하지만 작동하지 않는 : 폼으로

var i, formObj = document.form['addbookmark'], formObjLng = document.form['addbookmark'].length; 
// If the length property is undefined, then there is only one checkbox. 
if ((typeof formObjLng !== "undefined")) { 
    for (i = 0; i < formObjLng; i++) { 
     if ((formObj.elements['topic-link-item-' + i].type == "checkbox") && (formObj.elements['topic-link-item-' + i].checked)) { 
      params = params + "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i)).value; 
      params = params + "&topic-link-comment-box-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-comment-box-" + i)).value; 
     } 
    } 
} 

, 단순히 "addbookmark"의 ID와 양식의 , 그리고 이전에 내가 말한 것을 다시 말하면, 모든 것은 여기서 시도한 것을 제외하고는 모두 작동합니다.

+0

가 어디 "형태"당신이 언급되어있을 하는가? 실제 HTML 양식 요소가 있습니까? –

+1

양식 [POST] (http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) 또는 [GET] (http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods)이 맞습니까? 서버 측 코드가 POST를 기다리고 있다면 GET을 통해 매개 변수를 전달하는 것이 효과가 없을 것입니다. – JDB

+0

addbookmark 란 무엇입니까? 양식 요소 인 경우 form [ 'name']. 요소 [ 'elementname']. 형식을 사용하여 양식 요소 유형 (확인란 등)을 검색합니다. – KrisWebDev

답변

1

코드에 2 가지 문제가 있습니다. encodeURIComponent 기능을 사용하여 값을 URL 인코딩해야합니다. 합치 할 때 또한 당신은 params 변수에 결과를 다시 지정해야합니다 추가하는 다른 값에 대한

var params = 
    "title=" + encodeURIComponent(document.getElementById("title").value) + 
    "&url=" + encodeURIComponent(document.getElementById("url").value) + 
    "&snippet=" + encodeURIComponent(document.getElementById("snippet").value) + 
    "&tags=" + encodeURIComponent(document.getElementById("tags").value) + 
    "&status_bookmark=" + encodeURIComponent(document.getElementById("status_bookmark").value) + 
    "&comment=" + encodeURIComponent(document.getElementById("comment").value) + 
    "&status_comment=" + encodeURIComponent(document.getElementById("status_comment").value); 

도 :

var i, lng = document.getElementById('addbookmark').length; 
// If the length property is undefined, then there is only one checkbox. 
if (typeof lng === "undefined") { 
    params += "&topic-link-item-1=" + encodeURIComponent(document.getElementById("topic-link-item-1").value); 
    params += "&topic-link-comment-box-1=" + encodeURIComponent(document.getElementById("topic-link-comment-box-1").value); 
} 
else { 
    for (i = 0; i < lng; i++) { 
     params += "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); 
     params += "&topic-link-comment-box-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-comment-box-" + i).value); 
    } 
} 

공지 사항을 방법 :

params += "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); 

이는 다음과 같습니다 :

params = params + "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); 

당신이 처음에 무엇을하고 있는지와 동일하지 않습니다 :

params + "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); 

당신은 단순히 두 값을 연결 결코 다시 params 변수에 결과를 할당하지 않았다.

+0

안녕하세요, Darin, 제가 말씀 드렸듯이 첫 번째 매개 변수 시리즈가 작동합니다. 그것은 단지 두 번째 시리즈가 아닙니다. –

+0

아니요, 사용자가 해당 텍스트 상자에 위험한 문자를 입력하지 않았기 때문에 작동하고 있습니다. 그가 예를 들어 그 텍스트 상자 중 하나에'&'또는'= '기호를 입력하면 모든 것이 끊어집니다. 그래서'encodeURIComponent' 함수를 사용해야합니다. 두 번째 문제에 관한 한, 내 대답에 표시된 것처럼 params 변수에 결과를 다시 지정해야합니다. 'params + ... '대신에'params = params + ...'를 사용해야합니다. 이것이 코드가 작동하지 않는 이유입니다. –

+0

귀하의 추천 내용을 반영하도록 변경했습니다. –

1

Javascript는 텍스트가 당신이 그것을하는 방식을 추가하는 것을 허용하지 않는다고 확신합니다.

PARAMS = params를 +

+0

귀하의 추천 내용을 반영하도록 변경했습니다. –

관련 문제