我試過在這裏找到的各種東西,所以我不能得到這個工作。以下是我的整個HTML測試文檔。我用下面的網址加載它:無法使用javascript分配值
file:///Users/aaronbratcher/Documents/Tester.html?Donor.firstName=John&Donor.lastNameBusiness=Smith
我很期待是通過與在URL上傳遞的值的JavaScript來填充2個文本字段,但是他們沒有被填充。日誌消息表明一切都按預期發現。沒有報告錯誤或警告。
我在做什麼錯?
謝謝。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Accept Donation</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name = "viewport" content = "width = 320, initial-scale = 1, user-scalable = no">
</head>
<body>
<form name="AcceptDonation" action="http://localhost" onsubmit="return review()" method="get">
<H2>Donor Name</H2>
<div class="table-field">
<input type="text" class = "textField fullWidth" name = "Donor.firstName" id="firstName" autocorrect="off" placeholder="First Name">
</div>
<div class="table-field bottom">
<input type="text" class="textField fullWidth" name = "Donor.lastNameBusiness" id="lastName" autocorrect="off" placeholder="Last Name">
</div>
</form>
<script type='text/javascript'>
var urlParameters = {}
var kMinReceiptRequiredAmount = 250
window.onload = function() {
loadParameters()
}
function loadParameters() {
var url = window.location.search.substring(1)
var parts = url.split("&")
for (var i=0; i < parts.length; i++) {
var parameter = parts[i]
var parameterParts = parameter.split("=")
var parameterKey = decodeURI(parameterParts[0])
var parameterValue = decodeURI(parameterParts[1])
var docObject = document.getElementsByName(parameterKey)
console.log(docObject)
console.log(parameterValue)
if (docObject) {
docObject.value = parameterValue
}
}
}
</script>
</body>
</html>
工作你甚至記錄'docObject',問題可以清楚地在控制檯中看到。 – Teemu 2014-08-27 15:35:06
只有當你意識到括號表示一組對象時才顯而易見:P – 2014-08-27 15:39:30