2010-08-12 156 views
1

我是新來的阿賈克斯。我想在服務器上使用responseText填充窗體上的隱藏字段。我能夠將HTML中的responseText顯示爲innerHTML。我只是不確定如何填充表單上的隱藏字段。任何建議將不勝感激!Ajax - 如何填充隱藏字段?

:)

這裏的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; 
    } 
+0

+1爲了獲得用戶名Spockrates – 2010-08-12 17:56:21

回答

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

感謝您的回覆!所以給我的JS,我應該如何修改你的代碼來替換任何東西? – Spockrates 2010-08-12 17:54:42

+0

@Spock你想把迴應放在那裏嗎? – NullUserException 2010-08-12 17:59:01

+0

這項工作? var hiddenField = document.getElementById('someid'); hiddenField.value =('getLocation.responseText'); – Spockrates 2010-08-12 18:00:21

0

使用jQuery,它會設置值並更輕鬆地執行AJAX請求。

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

謝謝。我希望該值與getLocation.responseText相同。請告訴我應該怎麼做。 :) – Spockrates 2010-08-12 17:57:34

相關問題