开发者

Ajax - How Do I Populate a Hidden Field?

I'm new to Ajax. I'd like to populate a hidden field on a form with a responseText from the server. I'm able to display the responseText in the HTML as an innerHTML. I'm just unsure how to populate the hidden field on the form. Any suggestions will be greatly appreciated!

:)

Here's the 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:

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

JS:

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

You could change your function to:

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;
}


Use jQuery, it'll make setting the value and doing the AJAX request easier.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜