[英]javascript - Fill the value attribute of an input box
我正在嘗試制作一個簡單的webapp。 我想將主頁中的數據放入另一個窗口/選項卡上的輸入框中,但是當我打開它時輸入框始終為空。
這是代碼: global.js
function showUserInfo(event) {
// Prevent Link from Firing
event.preventDefault();
var _id = $(this).attr('rel');
var arrayPosition = userListData.map(function(arrayItem) { return arrayItem._id; }).indexOf(_id);
// Get our User Object
var thisUserObject = userListData[arrayPosition];
window.open('http://localhost:3000/editrecord', 'window', 'width=500,height=400');
var fName = thisUserObject.cName
$("#inputecName").attr('value', fname);
運行此命令將打開“ editrecord”窗口,而我要獲取的數據應位於“ editrecord”頁面的輸入框中。
editrecord.jade
#editBox
fieldset
input#inputecName(type='text', placeholder='Customer Name', width = '50%')
br
button#btnEditRec(type='submit') Update Record
先感謝您。
您可以通過URL發送數據,例如,
<a href="nextPage.html?data=blah">Next page</a>
通過JS
或服務器端腳本,您可以從URL提取data
並使用它,
你可以像這樣
var childWindow = window.open('http://localhost:3000/editrecord', 'window', 'width=500,height=400');
$(childWindow.document).load(function() {
var fName = thisUserObject.cName
$("#inputecName").attr('value', fname);
})
您可以使用普通javascript從inputecName
窗口訪問inputecName
childWindow.document.inputecName
Question上的js
嘗試在單獨的document
設置元素的value
$("#inputecName").attr('value', fname);
從當前document
,而不引用元素在單獨window
的單獨document
中。
您可以使用sessionStorage
在具有相同來源的瀏覽上下文或window
對象之間訪問Storage
對象。 在瀏覽器中打開editrecord.html
並加載document
時,利用.ready()
檢查sessionStorage
。
在原始的html
文檔global.js
<script>
sessionStorage.setItem("id", 123);
var w = window.open("editrecord.html", "w");
</script>
在editrecord.html
<head>
<script>
$(document).ready(function() {
if (sessionStorage.id) {
$("#inputecName").val(sessionStorage.getItem("id"))
}
})
</script>
</head>
<body>
<input type="text" id="inputecName" />
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.