2

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

:)

这是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;
    }
4

2 回答 2

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;
}
于 2010-08-12T17:49:37.713 回答
1

使用 jQuery,它将使设置值和执行 AJAX 请求更容易。

$("#something").attr("value", myvalue)
于 2010-08-12T17:50:40.067 回答