假設我在一個帶有按鈕的表單中有一個輸入:
<body>
<form class="contact-form" id="my_rec" name="my_rec">
<div class="container">
<div class="row">
<div class="col-sm-2">
<label for="TESTIMATED" class="col-form-label" style="font-size:10px;font-style:aril;font-weight:bold;text-align:right;color:blue;font-family:arial"><b>ESTIMATED COST:</b></label>
<p><input type="number" style="font-size:10px" class="form-control" id="TESTIMATED" name="TESTIMATED" value="0.00"></p>
<button name="btn2" type="button" class="btn btn-secondary" id="btn2">Close REC</button>
</div>
</div>
</div>
</form>
</body>
在表格之后我有一張桌子:
<br>
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="table-responsive table-sm">
<table id="tableCGRAL1" class="table table-hover table-bordered table-condensed" style="width:100%">
<thead class="text-center">
<tr bgcolor=blue class="firstrow" style="font-size:10px; font-style:arial;font-weight:bold;text-align:center; color:#FFFFFF; font-family:arial">
<th>Pda</th>
<th>Code</th>
<th>UM</th>
<th>Amount</th>
<th>Description</th>
<th>Unit cost</th>
<th>Estimated Cost</th>
<th>Calculated Cost</th>
<th>ACTIONS</th>
</tr>
</thead>
<tbody>
<tr style="font-style:arial;">
<td> <?php echo $_SESSION["Pda"]; ?></td>
<td> <?php if($resultw->num_rows > 0){ echo $roww['Code'];} ?></td>
<td> <?php if($resultg->num_rows > 0){ echo $rowg['UM']; } ?></td>
<td> <?php if($resultm->num_rows > 0){ echo $rowm['Amount']; } ?></td>
<td> <?php if($resultv->num_rows > 0){ echo $rowv['Description']; } ?></td>
<td> <?php if($resultq->num_rows > 0){ echo $rowq['UC']; } ?></td>
<td id="this_td" class="this_td"></td>
<td> <?php if($resultt->num_rows > 0){ echo $rowt['CC']; } ?></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
忽略 php 回顯,這只是為了顯示這些 td 從哪里獲取它們的值。
我的問題是,如果表單不在表格內,當我單擊按鈕時,如何使帶有 id 和類“this_td”的 td 具有上面表單中的輸入值。我無法在表格內移動表單并使用 row.(this).closest("tr") 因為我已經將該輸入用于其他用途,無法移動輸入。
問題是,我正在使用 table2csv https://github.com/OmbraDiFenice/table2csv 在單擊該按鈕時轉換表格,但是 td 是空的,我需要它具有用戶鍵入的輸入的值在,該值沒有時間去資料庫然后能夠用php將它帶回來,因為在它創建csv之前,它必須從客戶端獲取值(在頁面重新加載之前,因為按鈕提交并重新加載頁面)。
我怎樣才能用javascript做這個?我知道php,但我一點也不知道從哪里開始使用js。
uj5u.com熱心網友回復:
評論中提出的公認解決方案:
const td = document.getElementById("this_td")
const input = document.getElementById("TESTIMATED")
td.innerText = input.value
以防萬一您想讓它在您鍵入時作業,這將起作用:
const td = document.getElementById('this_td')
const input = document.getElementById('TESTIMATED')
input.addEventListener('keyup', (e) => {
td.innerText = input.value
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/532212.html
