Javascript html 表格单元格编辑不正确

我创建了一个包含 4 列的 html 表格。在该表中,其中一列是可编辑的(带有编辑按钮)。


当我单击编辑按钮时,如果我的 html 表中有 1 条记录,它会按预期工作。但如果我有 2 条记录,它就不会按预期工作。


当我单击第二条记录编辑按钮时,它使表中的第一条记录可编辑。


请在下面找到我的 html 代码。


<!DOCTYPE html>

<html>

<head>

<script>

    function update_country() {

        if(document.getElementById("Editbtn").value == 'Edit'){

            document.getElementById("country").style.display = 'none';

            document.getElementById("Editbtn").value = "Update";

            document.getElementById("countries").style.display = 'inline-block';

        }

    }


</script>

<style>

table {

  font-family: arial, sans-serif;

  border-collapse: collapse;

  width: 100%;

}


td, th {

  border: 1px solid #dddddd;

  text-align: left;

  padding: 8px;

}


tr:nth-child(even) {

  background-color: #dddddd;

}

</style>

</head>

<body>


<h2>HTML Table</h2>


<table>

  <tr>

    <th>ID</th>

    <th>Company</th>

    <th>Contact</th>

    <th>Country</th>

  </tr>

  <tr>

    <td>101</td>

    <td>Alfreds Futterkiste</td>

    <td>Maria Anders</td>

    <td id="country">Germany</td>

    <td id="countryOption">

    <select name="countries" id="countries" style="display:none;  >

    <option value="Germany">Germany</option>

    <option value="India">India</option>

    <option value="France">France</option>

    <option value="Italy">Italy</option>

    </select>

    <input id="Editbtn" type="button" value="Edit" onclick="update_country()"> 

    </td>

  </tr>


  <tr>

    <td>102</td>

    <td>Futterkiste</td>

    <td>Joe</td>

    <td id="country">Italy</td>

    <td id="countryOption">

    <select name="countries" id="countries" style="display:none;  >

    <option value="Germany">Germany</option>

    <option value="India">India</option>

    <option value="France">France</option>

    <option value="Italy">Italy</option>

    </select>

    <input id="Editbtn" type="button" value="Edit" onclick="update_country()"> 

    </td>

  </tr>

  

 </table>


</body>

</html>

http://img4.mukewang.com/6415297400012b3519140194.jpg

UYOU
浏览 132回答 1
1回答

鸿蒙传说

&nbsp;document.getElementById将始终返回与 id 匹配的第一个元素。所以你可以只抓住点击的目标:function update_country( e ) {&nbsp; if( e.target.value == 'Edit' ) {&nbsp; &nbsp; e.target.value = 'Update'&nbsp; &nbsp; e.target.parentNode.parentNode.querySelector( '#country' ).style.display = 'none'&nbsp; &nbsp; e.target.parentNode.querySelector( '#countries' ).style.display = 'inline-block'&nbsp; }}table {&nbsp; font-family: arial, sans-serif;&nbsp; border-collapse: collapse;&nbsp; width: 100%;}td, th {&nbsp; border: 1px solid #dddddd;&nbsp; text-align: left;&nbsp; padding: 8px;}tr:nth-child(even) {&nbsp; background-color: #dddddd;}<h2>HTML Table</h2><table>&nbsp; <tr>&nbsp; &nbsp; <th>ID</th>&nbsp; &nbsp; <th>Company</th>&nbsp; &nbsp; <th>Contact</th>&nbsp; &nbsp; <th>Country</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>101</td>&nbsp; &nbsp; <td>Alfreds Futterkiste</td>&nbsp; &nbsp; <td>Maria Anders</td>&nbsp; &nbsp; <td id="country">Germany</td>&nbsp; &nbsp; <td id="countryOption">&nbsp; &nbsp; &nbsp; <select id="countries" class="countries" style="display:none;">&nbsp; &nbsp; &nbsp; &nbsp; <option value="Germany">Germany</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="India">India</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="France">France</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="Italy">Italy</option>&nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; <input id="Editbtn" type="button" value="Edit" onclick="update_country(event)">&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>102</td>&nbsp; &nbsp; <td>Futterkiste</td>&nbsp; &nbsp; <td>Joe</td>&nbsp; &nbsp; <td id="country">Italy</td>&nbsp; &nbsp; <td id="countryOption">&nbsp; &nbsp; &nbsp; <select id="countries" class="countries" style="display:none;">&nbsp; &nbsp; &nbsp; &nbsp; <option value="Germany">Germany</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="India">India</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="France">France</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="Italy">Italy</option>&nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; <input id="Editbtn" type="button" value="Edit" onclick="update_country(event)">&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp;</table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript