2016-09-13 31 views
-1

我正在尋找完成寫入輸入後按Enter鍵的方式,我正在掃描輸入中的代碼條,但在代碼插入輸入後我要自動按輸入並呼叫我的功能。這是我對我的輸入代碼:我掃描了一個CodeBar後按Enter鍵

<div class="form-group"> 
     @Html.LabelFor(model => model.Id_Componente, "Id_Componente", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <input class="form-control" type="text" id="Id_Componente" name="Id_Componente" autofocus="autofocus" onkeypress="FunctionP(event)" /> 
      @Html.ValidationMessageFor(model => model.Id_Componente, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

但其唯一的工作,當我按在鍵盤上輸入,我想要做的東西后,我掃描了條形碼的自動調用我的函數:FunctionP(事件)。對不起我的英語不好。

+1

您可能可以使用輸入事件和去抖動,但是,我建議您查看掃描儀的文檔,我以前使用的文檔可以配置爲在輸入後導致輸入按鍵。 –

+2

許多掃描儀通常可配置爲在掃描後可選地添加終止字符(通常爲CR)。可能比軟件解決方案更容易 – Tibrogargan

回答

0

如果您無法從掃描儀讀取\r(回車)或\n(新行),則可以在字段中輸入足夠的字符時有條件地執行操作。例如,如果您的條形碼有32個字符:

$("#Id_Componente").keyup(function(e){ 
    if($(this).val().length == 32){ 
    // Do something 
    } 
}); 

如果您有可變長度,這將失敗。如果是這種情況,你可以尋找一個特定的字符作爲終結者。