ホームページ  >  記事  >  ウェブフロントエンド  >  js操作フォーム

js操作フォーム

php中世界最好的语言
php中世界最好的语言オリジナル
2018-04-19 10:10:061997ブラウズ

今回はjs操作フォームをお届けします。js操作フォームの注意事項は何ですか?実際のケースを見てみましょう。

実装アイデア:

1. 追加する場合: 現在のリストの行数を取得し、次の行を現在の行に追加します。
2. insertCell() メソッドを使用して行を追加します。添字は 0 から始まります。新しい行に型を追加してイベントに応答したい場合は、キーと値のペアと同様に setAttribute() メソッドを使用し、データを新しい行に保存するために appendChild() メソッドを使用します
4. : 削除する必要がある現在の行数を取得し、次に親ノードを取得し、行全体のデータを削除するのではなく、行全体を削除します
5.現在変更されている行の行番号インデックス。「変更」をクリックすると、テーブルのステータスがテキスト形式に変換され、「変更」が「OK」に変更されます。 実装コード:

<!DOCTYPE html>
<html>
  <head>
    <metacharset="UTF-8">
    <title></title>
    <style>
      table{
        border-top: 1px solid #ccc;
        border-left: 1px solid #ccc;
        width: 400px;
      }
      td,th{
        border-right:1px solid #ccc ;
        border-bottom: 1px solid #ccc;
      }
    </style>
    <script>
      function add(){
        var table = document.getElementById("order");
        var index = table.rows.length;//表格行数
        var row = table.insertRow(index);//插入一个行并返回新一行
 
 
        var c0 = row.insertCell(0);
        var b0 = document.createElement("input");
        b0.setAttribute("type","checkbox");
        b0.setAttribute("onclick","seclect("+index+")");
        b0.setAttribute("name","sel");
        c0.appendChild(b0);
 
        var c1 = row.insertCell(1);//在新一行插入一列,并返回新一列
        c1.innerHTML = prompt("请输入商品名称","");
 
        var c2 = row.insertCell(2);//在新一行插入一列,并返回新一列
        c2.innerHTML = prompt("输入数量","");
 
        var c3 = row.insertCell(3);//在新一行插入一列,并返回新一列
        c3.innerHTML = prompt("输入价格","");
 
        var c4 = row.insertCell(4);
        var b1 = document.createElement("input");
        b1.setAttribute("type","button");
        b1.setAttribute("value","删除");
        b1.setAttribute("onclick","del(this)");
 
        var b2 = document.createElement("input");//创建按钮
        b2.setAttribute("type","button");
        b2.setAttribute("value","修改");
        b2.setAttribute("style","margin-left: 5px");
        b2.setAttribute("onclick","update("+index+")");
 
        c4.appendChild(b1);//把按钮添加到操作的单元格中
        c4.appendChild(b2);
      }
 
      function del(but){
        //var table = document.getElementById("order");
        but.parentNode.parentNode.remove();//根据节点的层级关系删除行
      }
 
      function update(index){
        var table = document.getElementById("order");
        //获得修改按钮
        var cell=table.rows[index].cells[4];
 
        cell.lastChild.setAttribute("value","确定");
        //为按钮重新绑定事件
        cell.lastChild.setAttribute("onclick","edit("+index+")");
 
        //修改数量
        var cellNumer = table.rows[index].cells[2];
        var txt = document.createElement("input"); //创建一个文本框
        txt.setAttribute("value",cellNumer.innerHTML);//设置文本框的值
        txt.setAttribute("size",5);//文本框长度
        cellNumer.innerHTML = "";//把单元格的数据清除
        cellNumer.appendChild(txt); //把文本框加入到单元格
      }
 
      function edit(index){
        var table = document.getElementById("order");
 
        var cell = table.rows[index].cells[4];
 
        cell.lastChild.setAttribute("value","修改");
        cell.lastChild.setAttribute("onclick","update("+index+")");
 
        //把单元格中的文本框删除
        var cellNumer = table.rows[index].cells[2];
        var num = cellNumer.firstChild.value;//取文本框的值
        cellNumer.removeChild(cellNumer.firstChild);//删除文本框
        cellNumer.innerHTML = num;
      }
 
      function allSelect(ch){
        var item = document.getElementsByTagName("input"); //取所有的input标签
        for(var i=0;i<item.length;i++){ //循环每一个
          if(item[i].type==ch.type){ //判断每一个标签的类型是否为CheckBox
            item[i].checked= ch.checked; //复选框的选中与全选的复选框选中相同
          }
        }
      }
      function seclect(sh){
        varitem=document.getElementsByName("sel");
        varall=document.getElementById("all");
        vartag=true;
        for(vari=0;i<item.length;i++){//判断是否全部选中
          if(item[i].checked == false){
            tag=false;
            break;
          }
        }
        all.checked=tag;
      }
    </script>
  </head>
  <body>
    <center>
      <tableid="order">
        <tr>
          <th>
            <inputtype="checkbox"onclick="allSelect(this)"id="all"/>全选
          </th>
            <th>商品名称</th>
            <th>数量</th>
            <th>单价</th>
            <th>操作</th>
          </tr>
        <tr>
          <td><inputtype="checkbox"onclick="seclect(this)"name="sel"/></td>
          <td>娃哈哈</td>
          <td>10</td>
          <td>2</td>
          <td><inputvalue="删除"type="button"onclick="del(this)"style="margin-right:5px ;"/><inputvalue="修改"type="button"onclick="update(1)"/></td>
        </tr>
 
      </table>
      <buttononclick="add()">添加商品</button>
    </center>
  </body>
</html>

この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、PHP 中国語 Web サイトの他の関連記事に注目してください。

推奨読書:

モバイル側での画像カルーセルの js 実装

Vue スコープ スロットの使用方法の詳細な説明

以上がjs操作フォームの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。