Ajax - 如何填充隐藏字段?

发布于 2024-09-13 22:40:28 字数 705 浏览 2 评论 0原文

我是阿贾克斯新手。我想用来自服务器的响应文本填充表单上的隐藏字段。我能够将 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;
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-09-20 22:40:28

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

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;
}
如果没有你 2024-09-20 22:40:28

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

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

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

$("#something").attr("value", myvalue)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文