Ajax-如何填充隐藏字段?

Ajax-如何填充隐藏字段?,ajax,innerhtml,hidden-field,populate,responsetext,Ajax,Innerhtml,Hidden Field,Populate,Responsetext,我不熟悉Ajax。我想用服务器的responseText填充表单上的隐藏字段。我能够在HTML中以innerHTML的形式显示responseText。我只是不确定如何填充表单上的隐藏字段。如有任何建议,将不胜感激 :) 以下是JS: function getLocation(locationrouting) { var getLocation= newXMLHttpRequest(); // sending request getLocation.open("GET"

我不熟悉Ajax。我想用服务器的responseText填充表单上的隐藏字段。我能够在HTML中以innerHTML的形式显示responseText。我只是不确定如何填充表单上的隐藏字段。如有任何建议,将不胜感激

:)

以下是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;
    }
HTML:


使用jQuery,可以更轻松地设置值和执行AJAX请求

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

谢谢你的回复!因此,考虑到我的JS,我应该如何修改你的代码来替换任何东西?@Spock你想把响应放在那里吗?这样行吗?var hiddenField=document.getElementById('someid');hiddenField.value=('getLocation.responseText');是的,我确实想把它放在那里。也就是说,我希望页面上显示的responseText也是隐藏字段的值。这样,如果隐藏字段具有该值,我可以使用我的验证JS阻止表单提交。谢谢,亲爱的!比我想象的容易。谢谢。:)+1感谢用户名SpockRates感谢您。我希望该值与getLocation.responseText相同。请告诉我该怎么做。:)
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;
}
$("#something").attr("value", myvalue)