2017-08-08 17 views
0

私はHTMLテーブルの挿入、削除、追加行を処理するJavascriptページで作業しています。私は、フォームの行の値を変更するedit_row()関数を作成しています.Ajaxを使用してsave_row()関数を使用してそれらを保存するとします。 Platformという名前の列には、1,2,3、および4の4つの値しか指定できませんでした。入力の検証が容易なので、ドロップダウンを使用することにしました。そのため、ユーザーは1 2 3以外の値を挿入しません。 4.私はinnerHTMLの中にドロップダウン用のHTMLを置こうとしましたが、変わったように見えますし、JavaScriptにドロップダウンを入れる他の方法があるのでしょうか?getelementbyIDのinnerHTMLにドロップダウンを入れてください

function edit_row(id) 
{ 
    var companyname=document.getElementById("name"+id).innerHTML; 
    var destination=document.getElementById("destination"+id).innerHTML; 
    var time=document.getElementById("time"+id).innerHTML; 
    var platform=document.getElementById("destination"+id).innerHTML; 
    var date=document.getElementById("date"+id).innerHTML; 

    document.getElementById("name"+id).innerHTML="<input type='text' id='compname_text"+id+"' value='"+companyname+"'>"; 
    document.getElementById("destination"+id).innerHTML="<input type='text' id='age_text"+id+"' value='"+destination+"'>"; 
    document.getElementById("time"+id).innerHTML="<input type='time' id='time"+id+"' value='"+time+"'>"; 
    document.getElementById("platform"+id).innerHTML="<form id='platform"+id+"' value='"+platform+"'><select><option value = '12'> 1 </option ><option value = > 2</option></select>"; 
    document.getElementById("date"+id).innerHTML="<input type='date' id='date"+id+"' value='"+time+"'>"; 

    document.getElementById("edit_button"+id).style.display="none"; 
    document.getElementById("save_button"+id).style.display="block"; 

JSに来る必要がありますHTML::

<html> 
<head> 
    <link href = "style.css" rel = "stylesheet" type = "text/css"> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <title>Departures table for workers </title> 
    <script type="text/javascript" src="jquery-3.2.1.min.js"></script> 
    <script type="text/javascript" src="ajax.js"></script> 
</head> 
<body> 
<div class="table" align="center"> 
    <div style = "background-color:#FFFFFF; padding:20px;"><b>Departures Table for workers</b></div> 
    <table border="1" align="center" width="700"> 
     <thead> 
     <tr> 

      <th>ID</th> 
      <th>Train Company</th> 
      <th>Destination</th> 
      <th>Time</th> 
      <th>Platform</th> 
      <th>Date</th> 
     </tr> 
     </thead> 
     <tbody> 
     <?php while ($row = mysqli_fetch_assoc($result)): ?> 
      <tr> 
       <td><?php echo $row['id']?></td> 
       <td id="name"><?php echo $row['Traincompany'] ?></td> 
       <td id="destination"><?php echo $row['Destination'] ?></td> 
       <td id="time"><?php echo $row['Time'] ?></td> 
       <td id="platform"><?php echo $row['Platform'] ?></td> 
       <td id="date"><?php echo $row['Date'] ?></td> 
       <td> 
        <input type='button' class="edit_button" id="edit_button<?php echo $row['id'];?>" value="edit" onclick="edit_row('<?php echo $row['id'];?>');"> 
        <input type='button' class="delete_button" id="delete_button<?php echo $row['id'];?>" value="delete" onclick="delete_row('<?php echo $row['id'];?>');"> 
        <input type='button' class="save_button" id="save_button<?php echo $row['id'];?>" value="save" onclick="save_row('<?php echo $row['id'];?>');"> 
       </td> 
      </tr> 
     <?php endwhile ?> 
     <tr id="new_row"> 
      <td>New ID will be given automatically</td> 
      <td><input type="text" id="new_Traincompany" placeholder="Company name"></td> 
      <td><input type="text" id="new_Destination" placeholder="Destination"></td> 
      <td><input type="time" id="new_time"></td> 
      <td align="center"> 
        <select name = "Platform"> 
         <option value = '1'> 1 </option > 
         <option value = '2'> 2</option > 
         <option value = '3'> 3</option > 
         <option value = '4'> 4 </option > 
        </select> 
      </td> 
      <td><input type="date" id="new_date" ></td> 
      <td><input type="button" value="Insert Row" onclick="insert_row();"></td> 
     </tr> 
     </tbody> 
    </table> 
    <p class="message">Logout from user<a href="logout.php">Logout</a> 
</div> 
</body> 
</html> 

私は私のコードは本当に原始的かもしれません知っているので、私は提案を見たいのですがここで はedit_row()のコードでありますそれを改善する方法やJavascriptで行を編集する別の方法。

+0

さてあなたは、変数にものを保存し、クエリを繰り返し...例ように見える: 'companyname'は'のdocument.getElementById( "名前" + id)を含んでいる.innerHTML'しかし、あなたは単に変数にアクセスするのではなく、クエリを再度使用します...関連するHTMLをあなたのjavascriptに含めて、あなたが経験している問題を説明することができますか?* "よりも具体的ですが、正しくinnerHTMLに配置する "*ありがとうございます。 – NewToJS

+0

innerHTMLの部分を変数に入れるだけですか? – ProPall

答えて

0

まず、@ NewToJSのように、これらを専用変数に割り当てた直後にdocument.getElementById()。innerHTMLをもう一度呼び出す必要はありません。だから、コードは次のように簡略化することができます

function edit_row(id) 
{ 
    var companyname=document.getElementById("name"+id).innerHTML; 
    var destination=document.getElementById("destination"+id).innerHTML; 

    companyname="<input type='text' id='compname_text"+id+"' value='"+companyname+"'>"; 
    destination="<input type='text' id='age_text"+id+"' value='"+destination+"'>"; 
    ... 

次にあなたがinnerHTMLプロパティにすべてのコードを挿入することが好きではない場合は、ドロップダウンリストを作成し、メイン関数からそれを呼び出すための専用の関数を書くことができます。

function insertDropdown (quantity) { 
    var div = document.createElement('select'); 
    div.setAttribute("name", "platform"); 

    for (let i=0; i<quantity; i++) { 
    var option = document.createElement('option'); 
    option.setAttribute("value", i) 
    option.innerHTML = "option " + i 
    div.appendChild(option) 
    } 
    document.getElementById('dropdown-container').appendChild(div) 
} 

あなたがドロップダウンを挿入する場所を<td>のための適切なIDを割り当てられ、あなたのメイン関数から関数を呼び出していることを確認します。

function insertDropdown(quantity) { 
 
    var div = document.createElement('select'); 
 
    div.setAttribute("name", "platform"); 
 

 
    for (let i=0; i<quantity; i++) { 
 
    var option = document.createElement('option'); 
 
    option.setAttribute("value", i) 
 
    option.innerHTML = "option " + i 
 
    div.appendChild(option) 
 
    } 
 
    document.getElementById('dropdown-container').appendChild(div) 
 
} 
 

 
insertDropdown(3)
<table> 
 
    <tr> 
 
    <td id="dropdown-container"></td> 
 
    </tr> 
 
</table>

+0

これはどのように役立ちますか....これを使用して説明しなくても、JavaScriptの1行をダンプするとき、答えを受け入れることを真剣に受け入れることは、真剣に受け入れることができません。 – NewToJS

+0

これはかなり同じ著者が別の入力をしただけで動作します...申し訳ありませんが、ここで説明する必要があるものは実際にはわかりません。 – blewherself

+1

違いを説明し、違いが現在の問題を解決すべき理由は、OPがその変更の理由を理解し、現在のソースコードにどのように/どのような違いがあるのか​​を理解するソリューションを提供する点です。 – NewToJS

関連する問題