簡體   English   中英

javascript-填寫輸入框的value屬性

[英]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>

plnkr http://plnkr.co/edit/7TRQOPYPX4bMpxr6pjyX?p=預覽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM