2010-08-12 93 views
1

我是新来的阿贾克斯。我想在服务器上使用responseText填充窗体上的隐藏字段。我能够将HTML中的responseText显示为innerHTML。我只是不确定如何填充表单上的隐藏字段。任何建议将不胜感激!Ajax - 如何填充隐藏字段?

:)

这里的JS:

function getLocation(locationrouting) 
    { 
    var getLocation= newXMLHttpRequest(); // sending request 
    getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + locationrouting, false); 
    getLocation.send(null); // getting location 

    var dv = document.getElementById("location_div"); 
    var verifyUnfound() 

    if (getlocation.responseText === 'LOCATION NOT FOUND') 
    { 
     dv.style.color = "red"; 
    } 
     else 
    { 
     dv.style.color = "black"; 
    } 
    dv.innerHTML = getLocation.responseText; 
    } 
+0

+1为了获得用户名Spockrates – 2010-08-12 17:56:21

回答

1

HTML:

<input type="hidden" id="someid" name="somename" value="somevalue"> 

JS:

var hiddenField = document.getElementById('someid'); 
hiddenField.value = <whatever>; 

则可以将功能更改为:

function getLocation(locationrouting) { 
    var getLocation= newXMLHttpRequest(); // sending request 
    getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + locationrouting, false); 
    getLocation.send(null); // getting location 

    var dv = document.getElementById("location_div"); 
    var verifyUnfound(); 
    var hiddenField = document.getElementById('someid'); 

    if (getlocation . responseText === 'LOCATION NOT FOUND') { 
     dv.style.color = "red"; 
    } else { 
     dv.style.color = "black"; 
    } 
    dv.innerHTML = getLocation . responseText; 
    hiddenField.value = getLocation . responseText; 
} 
+0

感谢您的回复!所以给我的JS,我应该如何修改你的代码来替换任何东西? – Spockrates 2010-08-12 17:54:42

+0

@Spock你想把回应放在那里吗? – NullUserException 2010-08-12 17:59:01

+0

这项工作? var hiddenField = document.getElementById('someid'); hiddenField.value =('getLocation.responseText'); – Spockrates 2010-08-12 18:00:21

0

使用jQuery,它会设置值并更轻松地执行AJAX请求。

$("#something").attr("value", myvalue) 
+0

谢谢。我希望该值与getLocation.responseText相同。请告诉我应该怎么做。 :) – Spockrates 2010-08-12 17:57:34

相关问题