繁体   English   中英

在javascript / HTML(引导程序)中输入文本时按Enter键

[英]Pressing enter on a text input in javascript/HTML (bootstrap)

我已经尝试过这里发布的许多与表单有关的解决方案(例如,防止默认设置,更改表单属性),但是我似乎无法使其正常工作。 基本上,我从两个引导程序模板开始,到目前为止,一切进展顺利。 我现在有一个文本框,当您在第一个文本框中“提交”(即按Enter)时,希望添加另一个文本框。 目前,第一个文本输入由以下方式添加:

$('#item').after(
                    '<div class="form-group" onsubmit="return false"> <form class="form-horizontal" onSubmit="return false;"> </div><label for="exampleInputEmail1"> What was the value of the sale?</label><input type="text" class="form-control" id="InputSaleValue" placeholder="Enter value"><form onsubmit="return false"> <form action="javascript:void(-1)"> <small id="emailHelp" class="form-text text-muted">Excluding</small></div>'

但是此刻页面刚刚刷新,将我带到我尝试生成的此表单的第一部分。

我有这种模板。 我在这里发布了它,因为也许您可以将其用作垫脚石。 只需运行代码片段:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <html> <table> <tr> <td><button type="button" id="button1" style="margin: 10px;">Add Textbox</button> </tr> </table> </html> <script> $("#button1").click(function() { addRow(this); }); function addRow(btn) { var parentRow = btn.parentNode.parentNode; var table = parentRow.parentNode; var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var element1 = document.createElement("input"); element1.type = "text"; element1.name = "text1"; element1.className = "text1" cell1.appendChild(element1); } </script> 

在这里,您将获得示例解决方案https://jsfiddle.net/37h6c1rs/

 $('#item').append('<div class="form-group" onsubmit="return false"> <form class="form-horizontal" onSubmit="return false;"> </div><label for="exampleInputEmail1"> What was the value of the sale?</label><input type="text" class="form-control" id="InputSaleValue" placeholder="Enter value"><form onsubmit="return false"> <form action="javascript:void(-1)"> <small id="emailHelp" class="form-text text-muted">Excluding</small></div>'); $('#InputSaleValue').keypress(function(e){ if (e.which === 13) { $('#item').append('<div><input type="text id="addedTextbox /></div>') } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="item"></div> 

这可能不是您要查找的确切解决方案,但这将帮助您到达目的地。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM