使用jQuery / Javascript自动将数据填充到字段中

时间:2015-08-26 14:57:55

标签: javascript jquery swipe

我有一个护照扫描仪,它扫描文件并在每次扫描时打印出以下格式。它会开始把它写到它看到的任何开放文本区域。

START
Surname: SOMETHING
Forenames: NAME
Date of Birth: 10-10-91
Expiry Date:
Issue Date:
Document: PASSPORT
Doc. Number: 150150150
Issuing State: USA
Nationality: USA
Address Street:
Address City:
Address State:
Address Postal Code:
Address Country: USA
Height:
Weight:
Hair Color: BLACK
Eye Color: RED
END

信息进入文本字段,如下所示:

enter image description here

我想为这些信息准备字段,当我刷护照时,它会将它们全部填满。我从来没有过期这样的事情,寻找任何想法如何处理这个?

表单可能如下所示:

<form class="form-horizontal">
<fieldset>

<!-- Form Name -->
<legend>Passport scanner</legend>

<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="surname">Surname</label>
  <div class="controls">
    <input id="surname" name="surname" type="text" placeholder="" class="input-xlarge">

  </div>
</div>

<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="forenames">Forenames</label>
  <div class="controls">
    <input id="forenames" name="forenames" type="text" placeholder="" class="input-xlarge">

  </div>
</div>

</fieldset>
</form>

2 个答案:

答案 0 :(得分:1)

你可以使用一点jQuery从你的数据生成表单,虽然你可能真的需要一个静态表单 -

&#13;
&#13;
$(document).ready(function() {
  var passportArray = $("#scanned").val().trim().split("\n");
  passportArray.shift();
  passportArray.pop();
  for (var i = 0; i < passportArray.length; i++) {
    var line = passportArray[i].split(":");
    var name = line[0].trim().toLowerCase().replace(/ /g, "_");
    var $div = $("<div>").attr("class", "control-group");
    $div.append("<label>").attr({
      class: "control-label",
      for: name
    }).text(line[0]);
    var $innerDiv = $("<div>").attr("class", "controls");
    var $input = $("<input>").attr({
      id: name,
      name: name,
      type: "text",
      placeholder: "",
      class: "input-xlarge"
    }).val(line[1].trim());
    $innerDiv.append($input);
    $div.append($innerDiv);
    $("#passport_form fieldset").append($div);
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="scanned">
  START 
  Surname: SOMETHING 
  Forenames: NAME 
  Date of Birth: 10-10-91 
  Expiry Date: 
  Issue Date: 
  Document: PASSPORT 
  Doc. Number: 150150150 
  Issuing State: USA 
  Nationality: USA 
  Address Street: 
  Address City: 
  Address State: 
  Address Postal Code: 
  Address Country: USA 
  Height: 
  Weight: 
  Hair Color: BLACK 
  Eye Color: RED 
  END
</textarea>

<form id="passport_form" class="form-horizontal">
  <fieldset>

    <!-- Form Name -->
    <legend>Passport scanner</legend>

  </fieldset>
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用jQuery或只是简单的javascript填充字段,如下所示:

jQuery的:

$('#surname').val('surnameValue');

普通JavaScript:

document.getElementById('forenames').value = 'forenamesValue';

这是codepen