【问题标题】:How to auto select the dropdown如何自动选择下拉菜单
【发布时间】:2019-11-11 20:48:43
【问题描述】:

我有一个 HTML 表格,里面有几个输入字段,所以当我从 dropdown 中选择任何选项时,我将填充表格的一行,在该行 itemname 内作为输入字段 @987654326 @。

所以我正在做的是我有一个类似A BR SB EX~333 的字符串,其中 333 是项目代码,其他是名称,所以当我输入 333 时,该项目会被填充。然后按回车键,我开始专注并进行一些计算。

我要做什么

  • 当我在自动完成字段中输入例如333 并且只有一个选项时,我想在我的inputfield 中填充它。我不希望用户手动选择该选项,只需键入是否与单个 name 匹配,然后按回车键将其填充到输入字段中。

我的代码

var tableData = {
  "ALMOND CHBAR~2402": {
    "itemName": "ALMOND CHBAR",
    "itemCode": "2402",
    "costPrice": 20.0,
    "gstPercentage": 14.5,
    "mrp": 30.0
  },
  "A BR SB EX~333": {
    "itemName": "A BR SB EX",
    "itemCode": "333",
    "costPrice": 1.0,
    "gstPercentage": 0.0,
    "mrp": 1.0
  }
}


var autoCompleteData = Object.keys(tableData);

function rowappend(tbody) {

  const markup =
    `<tr>
									  <td>
									    <input type="text" class="form-control commanChange" id="itemNametd" name="itemNametd">
									  </td>
									  <td><input type="text" name="itemCodetd" id="itemCodetd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="mrptd" id="mrptd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="purRatetd" id="purRatetd" class="form-control commantd"></td>
									  <td>
									    <input type="tel" id="unitQtytd"class="form-control commanChange" name="unitQtytd">
									  </td>
						               			 
									  <td>
									    <input type="tel" id="discPercentagetd"class="form-control commanChange" name="discPercentagetd" >
									  </td>
									  <td><input type="text" name="discAmttd" id="discAmttd" class="form-control commantd" readonly="readonly"></td> 
									  <td><input type="text" name="gstPercentagetd" id="gstPercentagetd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="gstAmttd" id="gstAmttd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="totalAmttd" id="totalAmttd" class="form-control commantd" readonly="readonly"></td>
									  <td style="background-color: white;border: 1px white"><i class="fas fa-times fa-2x remove-btn"></i></td>
									  
									</tr>`

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

  $("[name=itemNametd]", tbody).last().autocomplete({
    source: autoCompleteData

  });




}
rowappend($('tbody', '#tableInvoice'))

function getValues(row) {

  const search = ($('[name=itemNametd]', row).val()).toString()
  const value = tableData[search];
  CostPrice = value.costPrice;

  if (value) {
    $(row).find("[name=itemCodetd]").val(value.itemCode);
    $(row).find("[name=mrptd]").val(value.mrp);
    $(row).find("[name=purRatetd]").val(CostPrice);

    $(row).find("[name=gstPercentagetd]").val(value.gstPercentage);
  }

}
document.addEventListener("keydown", function(e) {
  const row = e.target.parentElement.parentElement
  if (event.target.matches('[name=itemNametd]')) {

    var keycode = e.keyCode || event.e;
    if (keycode == '13') {

      getValues(e.target.parentElement.parentElement)
      $("[name=purRatetd]").focus();

    }
  }

});
$(document).on('focusout', (e) => {

  const row = e.target.parentElement.parentElement
  if (e.target.matches("[name=itemNametd]")) {


    getValues(e.target.parentElement.parentElement)
    $("[name=purRatetd]").focus();

  }
});
<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="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>

当我输入并且只剩下一个选项作为选项时,我希望它进入输入字段,这样用户就不会手动选择它。

如果有任何其他方法可以执行此自动完成,我愿意使用它,我只是希望在输入时填写输入字段,因为我不想手动选择它。

【问题讨论】:

  • @mkane 是的,我刚刚发现
  • 请注意,我们更喜欢这里的技术写作风格。我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,聊天材料和缩写 txtspk,恳求,你多久了被卡住、投票建议、元评论等。只需解释您的问题,并展示您尝试过的内容、预期的内容以及实际发生的情况。

标签: javascript jquery html autocomplete jquery-ui-autocomplete


【解决方案1】:

我在 j query 自动完成插件中找到了一些东西,即

autoSelectFirst: true,
 autoFocus: true

这是我正在努力实现的目标

var tableData = {
  "ALMOND CHBAR~2402": {
    "itemName": "ALMOND CHBAR",
    "itemCode": "2402",
    "costPrice": 20.0,
    "gstPercentage": 14.5,
    "mrp": 30.0
  },
  "A BR SB EX~333": {
    "itemName": "A BR SB EX",
    "itemCode": "333",
    "costPrice": 1.0,
    "gstPercentage": 0.0,
    "mrp": 1.0
  }
}


var autoCompleteData = Object.keys(tableData);

function rowappend(tbody) {

  const markup =
    `<tr>
									  <td>
									    <input type="text" class="form-control commanChange" id="itemNametd" name="itemNametd">
									  </td>
									  <td><input type="text" name="itemCodetd" id="itemCodetd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="mrptd" id="mrptd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="purRatetd" id="purRatetd" class="form-control commantd"></td>
									  <td>
									    <input type="tel" id="unitQtytd"class="form-control commanChange" name="unitQtytd">
									  </td>
						               			 
									  <td>
									    <input type="tel" id="discPercentagetd"class="form-control commanChange" name="discPercentagetd" >
									  </td>
									  <td><input type="text" name="discAmttd" id="discAmttd" class="form-control commantd" readonly="readonly"></td> 
									  <td><input type="text" name="gstPercentagetd" id="gstPercentagetd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="gstAmttd" id="gstAmttd" class="form-control commantd" readonly="readonly"></td>
									  <td><input type="text" name="totalAmttd" id="totalAmttd" class="form-control commantd" readonly="readonly"></td>
									  <td style="background-color: white;border: 1px white"><i class="fas fa-times fa-2x remove-btn"></i></td>
									  
									</tr>`

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

  $("[name=itemNametd]", tbody).last().autocomplete({
    source: autoCompleteData,
    autoSelectFirst: true,
    autoFocus: true

  });




}
rowappend($('tbody', '#tableInvoice'))

function getValues(row) {

  const search = ($('[name=itemNametd]', row).val()).toString()
  const value = tableData[search];
  CostPrice = value.costPrice;

  if (value) {
    $(row).find("[name=itemCodetd]").val(value.itemCode);
    $(row).find("[name=mrptd]").val(value.mrp);
    $(row).find("[name=purRatetd]").val(CostPrice);

    $(row).find("[name=gstPercentagetd]").val(value.gstPercentage);
  }

}
document.addEventListener("keydown", function(e) {
  const row = e.target.parentElement.parentElement
  if (event.target.matches('[name=itemNametd]')) {

    var keycode = e.keyCode || event.e;
    if (keycode == '13') {

      getValues(e.target.parentElement.parentElement)
      $("[name=purRatetd]").focus();

    }
  }

});
$(document).on('focusout', (e) => {

  const row = e.target.parentElement.parentElement
  if (e.target.matches("[name=itemNametd]")) {


    getValues(e.target.parentElement.parentElement)
    $("[name=purRatetd]").focus();

  }
});
<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="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>

我正在发布这个答案,但我也准备好使用其他方法

【讨论】:

    【解决方案2】:

    给你,你需要为此使用response回调

    var tableData = {
      "ALMOND CHBAR~2402": {
        "itemName": "ALMOND CHBAR",
        "itemCode": "2402",
        "costPrice": 20.0,
        "gstPercentage": 14.5,
        "mrp": 30.0
      },
      "A BR SB EX~333": {
        "itemName": "A BR SB EX",
        "itemCode": "333",
        "costPrice": 1.0,
        "gstPercentage": 0.0,
        "mrp": 1.0
      }
    }
    
    
    var autoCompleteData = Object.keys(tableData);
    
    function rowappend(tbody) {
    
      const markup =
        `<tr>
    									  <td>
    									    <input type="text" class="form-control commanChange" id="itemNametd" name="itemNametd">
    									  </td>
    									  <td><input type="text" name="itemCodetd" id="itemCodetd" class="form-control commantd" readonly="readonly"></td>
    									  <td><input type="text" name="mrptd" id="mrptd" class="form-control commantd" readonly="readonly"></td>
    									  <td><input type="text" name="purRatetd" id="purRatetd" class="form-control commantd"></td>
    									  <td>
    									    <input type="tel" id="unitQtytd"class="form-control commanChange" name="unitQtytd">
    									  </td>
    						               			 
    									  <td>
    									    <input type="tel" id="discPercentagetd"class="form-control commanChange" name="discPercentagetd" >
    									  </td>
    									  <td><input type="text" name="discAmttd" id="discAmttd" class="form-control commantd" readonly="readonly"></td> 
    									  <td><input type="text" name="gstPercentagetd" id="gstPercentagetd" class="form-control commantd" readonly="readonly"></td>
    									  <td><input type="text" name="gstAmttd" id="gstAmttd" class="form-control commantd" readonly="readonly"></td>
    									  <td><input type="text" name="totalAmttd" id="totalAmttd" class="form-control commantd" readonly="readonly"></td>
    									  <td style="background-color: white;border: 1px white"><i class="fas fa-times fa-2x remove-btn"></i></td>
    									  
    									</tr>`
    
      $(tbody).append(markup);
      setTimeout(() => $("[name=itemNametd]", tbody).last().focus(), 100);
    
      $("[name=itemNametd]", tbody).last().autocomplete({
        source: autoCompleteData,
        response: (e, ui)=>{
          if(ui.content.length === 1){
            $(e.target).val(ui.content[0].label);
            $(e.target).autocomplete( "close" );
          }
        }
      });
    
    
    
    
    }
    rowappend($('tbody', '#tableInvoice'))
    
    function getValues(row) {
    
      const search = ($('[name=itemNametd]', row).val()).toString()
      const value = tableData[search];
      CostPrice = value.costPrice;
    
      if (value) {
        $(row).find("[name=itemCodetd]").val(value.itemCode);
        $(row).find("[name=mrptd]").val(value.mrp);
        $(row).find("[name=purRatetd]").val(CostPrice);
    
        $(row).find("[name=gstPercentagetd]").val(value.gstPercentage);
      }
    
    }
    document.addEventListener("keydown", function(e) {
      const row = e.target.parentElement.parentElement
      if (event.target.matches('[name=itemNametd]')) {
    
        var keycode = e.keyCode || event.e;
        if (keycode == '13') {
    
          getValues(e.target.parentElement.parentElement)
          $("[name=purRatetd]").focus();
    
        }
      }
    
    });
    $(document).on('focusout', (e) => {
    
      const row = e.target.parentElement.parentElement
      if (e.target.matches("[name=itemNametd]")) {
    
    
        getValues(e.target.parentElement.parentElement)
        $("[name=purRatetd]").focus();
    
      }
    });
    <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="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>

    【讨论】:

    • 嘿,我发现使用j query 插件我认为这将是完美的
    • 是的,你的方法也行得通,但我认为使用上述方法对我的工作来说是完美的
    • 如果您认为您的方法解决了您的问题,请使用您的方法,否则我为您提供了最佳解决方案,无需任何额外的插件或库
    • 是的,谢谢你,我会为你的答案投票
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2014-12-11
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    相关资源
    最近更新 更多