如何专注于输入第一字段

时间:2019-06-20 05:33:42

标签: javascript jquery css html-table

我有一个复杂的任务要做,几乎已经完成,但是只停留在一个地方

我在做什么

  • 我的UI上有3个输入字段,后跟一个HTML表格,“输入”字段是简单的输入字段,用户将在其中输入一些内容
  • 在我的HTML表中,我有几个输入字段以获取用户输入,最初第一行只有autocomplete的一行,用户从中选择一些数据,并据此填充下一列这些行中的
  • 在我的情况下,当最后一个输入字段为Disc%时,因此当用户focusout来自此时,我正在创建具有相同功能的新行,并且焦点应移至该新行的第一列,即itemName

我的问题是什么

  • 当我加载页面时,焦点自动聚焦在不需要的表内的itemName上

  • 当页面加载焦点时,应首先将焦点放在表外的输入字段上,然后如果它是focusout之后tble之外的最后一个输入字段,则应进入表的输入字段

    < / li>
  • 在我的代码中,我认为setTimeout(() => $("[name=itemNametd]", tbody).last().focus(), 100);这行引起了问题,因为当用户focusout来自HTML表的最后一个输入字段时,我正在创建新行并将焦点移到新行的第一列上

  • 我评论了我认为问题所在的行

"use strict";
console.clear()


const data = [ //data to populate Item Name search input field
  {
    "ItemName": "Butter"
  },
  {
    "ItemName": "Rice"
  },
  {
    "ItemName": "Milk"
  },
  {
    "ItemName": "Ice Cream"
  },
  {
    "ItemName": "Curd"
  }
]

const data1 = { // this data will be dynamic but for now to test i am using this single data
  butter: {
    "ItemName": "Butter",
    "ItemCode": 400564,
    "PurRate": 8,
    "DiscAmt": 6,
    "gstPercentage": 35,
    "gstAmt": 5
  },
  rice: {
    "ItemName": "Rice",
    "ItemCode": 400565,
    "PurRate": 3,
    "DiscAmt": 2,
    "gstPercentage": 20,
    "gstAmt": 8
  },
  milk: {
    "ItemName": "Milk",
    "ItemCode": 200569,
    "PurRate": 1,
    "DiscAmt": 1,
    "gstPercentage": 50,
    "gstAmt": 2
  },
  'ice cream': {
    "ItemName": "Ice cream",
    "ItemCode": 800002,
    "PurRate": 16,
    "DiscAmt": 2,
    "gstPercentage": 15,
    "gstAmt": 2
  },
  curd: {
    "ItemName": "Curd",
    "ItemCode": 100289,
    "PurRate": 9,
    "DiscAmt": 1,
    "gstPercentage": 12,
    "gstAmt": 4
  },
}

var totalAmount = "";
var unitQuantity = "";

$("#supplierInput").on('input', function() {
  myCode();
});

function myCode() {
  function rowappend(tbody) { // this one is appending row{

    const markup =
      `<tr>
  <td>
    <input type="text" class="form-control commantd" name="itemNametd">
  </td>
  <td name="itemCodetd" class="commantd"></td>
  <td>
    <input type="text" class="form-control commantd" name="unitQtytd">
  </td>
  <td name="purRatetd" class="commantd"></td>
  <td>
    <input type="text" class="form-control commantd" name="discPercentagetd">
  </td>
  <td name="discAmttd" class="commantd"></td> 
  <td name="gstPercentagetd" class="commantd"></td>
  <td name="gstAmttd" class="commantd"></td>
  <td name="totalAmttd" class="commantd"></td>
  
</tr>`

    $(tbody).append(markup);
    setTimeout(() => $("[name=itemNametd]", tbody).last().focus(), 100);

    const itemName = data.map(value => { //using autocomplete to for searching input field
      return value.ItemName;
    });
    $("[name=itemNametd]", tbody).last().autocomplete({
      source: itemName
    });
  }
  rowappend($('tbody', '#tableInvoice'))


  function getValues(row) {
    const search = ($('[name=itemNametd]', row).val()).toString()
    const value = data1[search.toLowerCase()];
    if (value) {
      $(row).find("[name=itemCodetd]").text(value.ItemCode);
      $(row).find("[name=purRatetd]").text(value.PurRate);
      $(row).find("[name=discAmttd]").text(value.DiscAmt);
      $(row).find("[name=gstPercentahgetd]").text(value.gstPercentage);
      $(row).find("[name=gstAmttd]").text(value.gstAmt);
    }
  }



  function calc(row) {
    const unitQuantity = $(row).find("[name=unitQtytd]").val();
    const purchaseRate = $(row).find("[name=purRatetd]").text();
    const totalAmount = (parseInt(unitQuantity) * parseInt(purchaseRate));

    $(row).find("[name=totalAmttd]").text(totalAmount);

  }

  $(document).on('focusout', (e) => {
    const row = e.target.parentElement.parentElement
    if (e.target.matches('[name=discPercentagetd]')) {
      if ($(row).parent().find('tr').length - $(row).index() === 1) { // only last row
        rowappend(e.target.parentElement.parentElement.parentElement)
      }
    }

    if (e.target.matches('[name=unitQtytd]')) {
      calc(e.target.parentElement.parentElement)
    }

    if (e.target.matches("[name=itemNametd]")) {
      getValues(e.target.parentElement.parentElement)
    }

  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />

<div class="container commonDivInvoice">
  <div class="form-row">
    <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
      <label for="dcNoInput">DC No</label> <input type="text" class="form-control" name="dcNoInput" id="dcNoInput">
    </div>
    <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
      <label for="supplierInput">Supplier</label> <input list="supplierInputList" id="supplierInput" class="form-control custom-select ">
      <datalist id="supplierInputList">
  <option>1</option>
  <option>2</option>
						</datalist>
    </div>
    <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
      <label for="dcInput">DC Input</label> <input type="text" class="form-control" name="dcInput" id="dcInput">
    </div>




  </div>
  <div class="row tableInvoice" id="commonDvScroll">
    <table class="table table-bordered" id="tableInvoice">
      <thead>
        <tr>
          <th id="itemNameth" class="commanth">Item Name</th>
          <th id="itemCodeth" class="commanth">Item Code</th>
          <th id="unitQtyth" class="commanth">Unit Qty</th>
          <th id="purRateth" class="commanth">Pur.Rate</th>
          <th id="discPercentageth" class="commanth">Disc%</th>
          <th id="discAmtth" class="commanth">Disc Amt</th>
          <th id="gstPercentageth" class="commanth">Gst%</th>
          <th id="gstAmtth" class="commanth">Gst Amt</th>
          <th id="totalAmtth" class="commanth">Total Amount</th>
        </tr>
      </thead>
      <tbody>

      </tbody>

    </table>

  </div>
</div>

编辑/更新

  • 我用正确的数据编辑了代码,我使用了datalist标签,因此更改时我正在创建表格行,因此当我更改datalist时,焦点将移至表格行
  • datalist之后,我实际上有这样的结构:我有很多输入字段要填写,然后想上表

2 个答案:

答案 0 :(得分:1)

只需使用$("#dcNoInput").focus(), 100);专注于该文本框即可。

我所做的就是获取具有其id的textbox元素并使用了.focus()

"use strict";
console.clear()
$("#dcNoInput").focus();

const data = [ //data to populate Item Name search input field
  {
    "ItemName": "Butter"
  },
  {
    "ItemName": "Rice"
  },
  {
    "ItemName": "Milk"
  },
  {
    "ItemName": "Ice Cream"
  },
  {
    "ItemName": "Curd"
  }
]

const data1 = { // this data will be dynamic but for now to test i am using this single data
  butter: {
    "ItemName": "Butter",
    "ItemCode": 400564,
    "PurRate": 8,
    "DiscAmt": 6,
    "gstPercentage": 35,
    "gstAmt": 5
  },
  rice: {
    "ItemName": "Rice",
    "ItemCode": 400565,
    "PurRate": 3,
    "DiscAmt": 2,
    "gstPercentage": 20,
    "gstAmt": 8
  },
  milk: {
    "ItemName": "Milk",
    "ItemCode": 200569,
    "PurRate": 1,
    "DiscAmt": 1,
    "gstPercentage": 50,
    "gstAmt": 2
  },
  'ice cream': {
    "ItemName": "Ice cream",
    "ItemCode": 800002,
    "PurRate": 16,
    "DiscAmt": 2,
    "gstPercentage": 15,
    "gstAmt": 2
  },
  curd: {
    "ItemName": "Curd",
    "ItemCode": 100289,
    "PurRate": 9,
    "DiscAmt": 1,
    "gstPercentage": 12,
    "gstAmt": 4
  },
}

var totalAmount = "";
var unitQuantity = "";

$("#supplierInput").on('input', function() {
  var dcInput = $("#dcInput").val();

    myCode();
  if(!dcInput){
    setTimeout(() => $("#dcInput").focus(), 110);
  }
});

function myCode() {
  function rowappend(tbody) { // this one is appending row{

    const markup =
      `<tr>
  <td>
    <input type="text" class="form-control commantd" name="itemNametd">
  </td>
  <td name="itemCodetd" class="commantd"></td>
  <td>
    <input type="text" class="form-control commantd" name="unitQtytd">
  </td>
  <td name="purRatetd" class="commantd"></td>
  <td>
    <input type="text" class="form-control commantd" name="discPercentagetd">
  </td>
  <td name="discAmttd" class="commantd"></td> 
  <td name="gstPercentagetd" class="commantd"></td>
  <td name="gstAmttd" class="commantd"></td>
  <td name="totalAmttd" class="commantd"></td>
  
</tr>`

    $(tbody).append(markup);
    setTimeout(() => $("[name=itemNametd]", tbody).last().focus(), 100);

    const itemName = data.map(value => { //using autocomplete to for searching input field
      return value.ItemName;
    });
    $("[name=itemNametd]", tbody).last().autocomplete({
      source: itemName
    });
  }
  rowappend($('tbody', '#tableInvoice'))


  function getValues(row) {
    const search = ($('[name=itemNametd]', row).val()).toString()
    const value = data1[search.toLowerCase()];
    if (value) {
      $(row).find("[name=itemCodetd]").text(value.ItemCode);
      $(row).find("[name=purRatetd]").text(value.PurRate);
      $(row).find("[name=discAmttd]").text(value.DiscAmt);
      $(row).find("[name=gstPercentahgetd]").text(value.gstPercentage);
      $(row).find("[name=gstAmttd]").text(value.gstAmt);
    }
  }



  function calc(row) {
    const unitQuantity = $(row).find("[name=unitQtytd]").val();
    const purchaseRate = $(row).find("[name=purRatetd]").text();
    const totalAmount = (parseInt(unitQuantity) * parseInt(purchaseRate));

    $(row).find("[name=totalAmttd]").text(totalAmount);

  }

  $(document).on('focusout', (e) => {
    const row = e.target.parentElement.parentElement
    if (e.target.matches('[name=discPercentagetd]')) {
      if ($(row).parent().find('tr').length - $(row).index() === 1) { // only last row
        rowappend(e.target.parentElement.parentElement.parentElement)
      }
    }

    if (e.target.matches('[name=unitQtytd]')) {
      calc(e.target.parentElement.parentElement)
    }

    if (e.target.matches("[name=itemNametd]")) {
      getValues(e.target.parentElement.parentElement)
    }

  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />

<div class="container commonDivInvoice">
  <div class="form-row">
    <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
      <label for="dcNoInput">DC No</label> <input type="text" class="form-control" name="dcNoInput" id="dcNoInput">
    </div>
    <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
      <label for="supplierInput">Supplier</label> <input list="supplierInputList" id="supplierInput" class="form-control custom-select ">
      <datalist id="supplierInputList">
  <option>1</option>
  <option>2</option>
						</datalist>
    </div>
    <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
      <label for="dcInput">DC Input</label> <input type="text" class="form-control" name="dcInput" id="dcInput">
    </div>




  </div>
  <div class="row tableInvoice" id="commonDvScroll">
    <table class="table table-bordered" id="tableInvoice">
      <thead>
        <tr>
          <th id="itemNameth" class="commanth">Item Name</th>
          <th id="itemCodeth" class="commanth">Item Code</th>
          <th id="unitQtyth" class="commanth">Unit Qty</th>
          <th id="purRateth" class="commanth">Pur.Rate</th>
          <th id="discPercentageth" class="commanth">Disc%</th>
          <th id="discAmtth" class="commanth">Disc Amt</th>
          <th id="gstPercentageth" class="commanth">Gst%</th>
          <th id="gstAmtth" class="commanth">Gst Amt</th>
          <th id="totalAmtth" class="commanth">Total Amount</th>
        </tr>
      </thead>
      <tbody>

      </tbody>

    </table>

  </div>
</div>

答案 1 :(得分:0)

非常简单。将这行代码写到document.ready()中,让我知道。

$("#YourFormId").find('input[type="text"]').first().focus();