javascript-如何根据在其他文本字段中输入的名称自动填充ID字段?
我有两个文本字段:javascript-如何根据在其他文本字段中输入的名称自动填充ID字段?,javascript,html,Javascript,Html,我有两个文本字段: <form action="input.php" method="post"> <!--First Field--> <input type="text" name="id" id="id" value="" /> <!--Second Field--> <input type="text" name="name" id="name" value="" /> </form> 所以,当我在
<form action="input.php" method="post">
<!--First Field-->
<input type="text" name="id" id="id" value="" />
<!--Second Field-->
<input type="text" name="name" id="name" value="" />
</form>
所以,当我在第二个字段输入员工姓名时,第一个字段将自动填写员工ID
例如:
Second Field : Edward
First Field : 1
我已经使用了这段代码,但它只复制了我在第二个字段中键入的内容
$(function(){
$("#name").keypress(function(){
$("#id").val($(this).val());
});
});
那么,我必须编写什么样的javascript代码?谢谢师父:您可以这样做
$(function () {
// Handler for .ready() called.
$("input[type='text']").each(function(){
$(this).attr("id",$(this).attr("name") + "_staff");
});
});
请提供更多关于您想要实现的目标的信息。这个“id”依赖于什么?它与这个名字有什么关系?数据来自哪里?这整个问题没有任何意义=/要么预加载数据库内容所有值的json值(如果不是很多的话),要么以另一种方式在keyup上使用ajax
$(function () {
// Handler for .ready() called.
$("input[type='text']").each(function(){
$(this).attr("id",$(this).attr("name") + "_staff");
});
});