打開新窗口 參數(shù)通過form的post傳遞

  • window.open打開新窗口,通過post傳遞參數(shù)
  • 新窗口通過opener.document.formName.poNum.value獲取參數(shù)
  • opener.document代表父類

parent.tsx

let num = 0;

const getWriteDownDetail = (num: Number) => {
  const url = `/son/${num}`;
  //首先創(chuàng)建一個form表單  
  var tempForm = document.createElement("form");
  tempForm.id = `myform${num}`;
  //制定發(fā)送請求的方式為post  
  tempForm.method = "post";
  tempForm.name = `myform${num}`;
  //此為window.open的url,通過表單的action來實現(xiàn)  
  tempForm.action = url;
  //利用表單的target屬性來綁定window.open的一些參數(shù)(如設(shè)置窗體屬性的參數(shù)等)  
  tempForm.target = "_blank";
  //創(chuàng)建input標簽,用來設(shè)置參數(shù)  
  var hideInput = document.createElement("input");
  hideInput.type = "hidden";
  hideInput.name = "poNum";
  hideInput.value = `yangyang${num}`;
  //將input表單放到form表單里  
  tempForm.appendChild(hideInput);
  //將此form表單添加到頁面主體body中  
  document.body.appendChild(tempForm);
  //手動觸發(fā),提交表單  
  tempForm?.submit();
  //從body中移除form表單  
  // document.body.removeChild(tempForm);
}

getWriteDownDetail(1)

son.jsx

const { pathname } = history.location;
const num = pathname.replace('/son/', '');
const formName = `myform${num}`;
// 通過form傳遞過來的參數(shù)
const res = opener.document[formName].poNum.value;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容