首页 > 解决方案 > 更改 onload xmlhttprequest javascript 上的表单值?

问题描述

    if (xhr.status === 200) {
        alert(xhr.responseText+document.getElementById("thatid").value);
        document.getElementById("thatid").value = xhr.responseText;
        //document.getElementById("f3").value = 'yayayay';
    }

<form>
<button id="thatbtn" value="submit" >
<input type="hidden" id="thatid" value="bob" >
</form>

基本上警报有效并打印 xhr.responsetext,但它不会更改表单上的“thatid”输入

<div id="f5" style="display:none;"></div>

xhr.onload = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
        dude = xhr.responseText;
        f5.innerHTML = dude;
        alert(dude+f5.innerHTML);
         //this does change the innerHTML
         //however, trying to read f5.innerHTML outside of onload
          //function will read '' or none, y not persistent?
    }
    else if (xhr.status !== 200) {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};

警报(f5.innerHTML);//没有文本

标签: javascripthtml

解决方案


推荐阅读