2016-11-01 5 views
0

를 사용에서 getElementById 방법 :내가 사업부는 DOM 내가 아약스를 사용하여 얻을 JS 변수를 사용하고이를 사용하려면 포스트 를 사용하여 Ajax 호출로 다시 전송 DIV 이름의 PHP를 사용 ajax.response

idisis=ajax.responseText; 
document.getElementById(idisis).innerHTML= some text; 
// ('idisis') || (''+idisis+'')|| ("'+idisis+'") 

하지만 작동하지 않습니다. 것이 가능하다 ?

+1

"작동하지 않는다"는 것은 무엇을 의미합니까? 정확히 무엇이 잘못 될까요? 이 경우 아약스는 무엇입니까? 당신이해야하는 것처럼 아약스 콜백 콜백에서 이것을하지 않을 것이라고 추측합니다. 또한 "일부 텍스트는"따옴표 – Pabs123

+0

'ajax.onreadystatechange = 함수() { \t을해야하는 경우 (ajaxReturn (아약스) == TRUE) { (ajax.responseText) 경우 { idisis = ajax.responseText; document.getElementById (idisis) .innerHTML = "some text"; } \t} } '아무 일도 일어나지 않습니다. –

답변

0

여기는 내가 작성한 원시 XMLHttpRequest 포스트 기능과 몇 가지 다른 기능이 있습니다. 이전 버전과 호환됩니다.

function phpEncode(obj){ 
    var r = []; 
    if(obj instanceof Array){ 
    for(var i=0,l=obj.length; i<l; i++){ 
     r.push(phpEncode(obj[i])); 
    } 
    return '%5B'+r.join(',')+'%5D'; 
    } 
    else if(typeof obj === 'object' && obj){ 
    for(var i in obj){ 
     if(obj.hasOwnProperty(i)){ 
     var v = obj[i], s; 
     if(typeof v === 'object' && v){ 
      s = encodeURIComponent('"'+i.replace('"', '\\"')+'":')+phpEncode(v); 
     } 
     else{ 
      v = typeof v === 'string' ? '"'+v.replace('"', '\"')+'"' : v; 
      s = encodeURIComponent('"'+i.replace('"', '\\"')+'":'+v); 
     } 
     r.push(s); 
     } 
    } 
    return '%7B'+r.join(',')+'%7D'; 
    } 
    else{ 
    r = typeof obj === 'string' ? '"'+obj.replace('"', '\\"')+'"' : obj; 
    return ''+r; 
    } 
} 
function phpAccept(respText){ 
    return eval('('+decodeURIComponent(respText)+')'); 
} 
function clone(obj){ 
    return phpAccept(phpEncode(obj)); 
} 
function post(send, where, success, context){ 
    var x = new XMLHttpRequest || new ActiveXObject('Microsoft.XMLHTTP'); 
    var c = context || this; 
    x.open('POST', where); x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
    x.onreadystatechange = function(){ 
    if(x.readyState === 4 && x.status === 200){ 
     if(success)success.call(c, phpAccept(x.responseText)); 
    } 
    } 
    if(send && typeof send === 'object' && !(send instanceof Array)){ 
    var r = []; 
    for(var p in send){ 
     r.push(encodeURIComponent(p)+'='+phpEncode(send[p])); 
    } 
    if(r.length)x.send(r.join('&')); 
    } 
    return x; 
} 
post({testProperty:'testValue'}, 'yourPage.php', function(response){ 
    // evaluate response and run all your other code here 
});