You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按列表遍历表格tr、拆分登录账号与密码并在表格中打印两者值

Answers to Your Table Manipulation & Code Fix Questions

Hey there! Let's tackle your questions step by step, plus fix those code bugs you mentioned.

1. How to Iterate Through <tr> Elements in a Table

You can use native JavaScript DOM methods to grab all table rows and loop through them. Here's a straightforward example:

// Grab all <tr> elements in your table (add :not(:first-child) if you want to skip the header row)
const tableRows = document.querySelectorAll('table tr');

// Loop through each row
tableRows.forEach((row, index) => {
  console.log(`Row ${index + 1}:`, row);
  // Add your row-specific logic here, like grabbing <td> content
});

If you're using jQuery, it's just $('table tr').each(function() { /* your code */ }); but native JS is more lightweight these days.

2. How to Split Login ID and Password

First, you need a consistent separator in your combined string (like |, :, or a space). Use the split() method to break it into two parts:

// Example: Combined string with a | separator
const combinedCredentials = "jane_smith|myStrongPass789";
// Destructure the split result into loginId and password
const [loginId, password] = combinedCredentials.split('|');

console.log('Login ID:', loginId);
console.log('Password:', password);

Add a quick validation to handle bad formatting:

function splitCredentials(str, separator = '|') {
  const parts = str.split(separator);
  if (parts.length !== 2) {
    throw new Error("Invalid format! Make sure your login ID and password are separated by the correct character.");
  }
  return { loginId: parts[0], password: parts[1] };
}

3. How to Print Login ID & Password to a Table

You can create new table rows and cells dynamically, then append them to your table. Note: Never display plaintext passwords in production—this is just for demo purposes!

// Grab your target table
const targetTable = document.querySelector('table');
// Create a new row
const newRow = document.createElement('tr');

// Create Login ID cell
const loginCell = document.createElement('td');
loginCell.textContent = loginId;
newRow.appendChild(loginCell);

// Create Password cell (again, hide this in real apps!)
const passwordCell = document.createElement('td');
passwordCell.textContent = password;
newRow.appendChild(passwordCell);

// Add the new row to the table
targetTable.appendChild(newRow);

If you need to update an existing row instead, find the row by index or ID, then modify its <td> elements' textContent.


Fixes for Your Existing Code Issues

Let's fix those bugs you pointed out:

  • Reusing loginIdVal for password element:
    Wrong:

    const password = document.getElementById(loginIdVal); // Using login ID value instead of password element ID
    

    Fixed:

    // Replace 'password' with your actual password element's ID
    const passwordElement = document.getElementById('password');
    const passwordVal = passwordElement.value;
    
  • Using push instead of list in alert:
    Wrong:

    alert('List content: ' + push); // Typo—should reference your list variable
    

    Fixed:

    // Use join() to format the list nicely
    alert('List content: ' + list.join(', '));
    
  • Misspelled methode in form tag:
    Wrong:

    <form methode="POST">...</form>
    

    Fixed:

    <form method="POST">...</form> <!-- Correct spelling is "method" -->
    

内容的提问来源于stack exchange,提问作者Samudrala Ramu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:15:38