Javascript 更改具有相同id的输入类型的所有值

Javascript 更改具有相同id的输入类型的所有值,javascript,jquery,dom,Javascript,Jquery,Dom,我现在遇到的问题是,我想更改id与输入类型匹配的每个元素的值。请看我的JSFIDLE,这对你来说应该很清楚了。问题是,所有输入类型的值都不会更改,它只更改该id的第一个输入类型。尝试使用.each函数,但没有帮助 HTML <input type="text" id="first" value="tesdafsdf" /> <input type="text" id="second" /> <input type="text" id="second" /> &

我现在遇到的问题是,我想更改id与输入类型匹配的每个元素的值。请看我的JSFIDLE,这对你来说应该很清楚了。问题是,所有输入类型的值都不会更改,它只更改该id的第一个输入类型。尝试使用.each函数,但没有帮助

HTML

<input type="text" id="first" value="tesdafsdf" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="first" value="tesdafsdf" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />

不幸的是,HTML元素id需要不同,它们是唯一的标识符。但是,类可以应用于多个元素,如下所示:

HTML

<input type="text" id="first" value="tesdafsdf" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="first" value="tesdafsdf" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />

不幸的是,HTML元素id需要不同,它们是唯一的标识符。但是,类可以应用于多个元素,如下所示:

HTML

<input type="text" id="first" value="tesdafsdf" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="second" />
<input type="text" id="first" value="tesdafsdf" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="text" class="second" />
用于唯一标识元素,因此元素的ID必须唯一

它在文档中必须是唯一的,并且通常用于检索 元素使用。id的其他常见用法包括使用 在使用设置文档样式时,该元素将被删除

当您必须对多个元素进行分组时,最简单的方法之一就是使用class属性

使用id选择器时,它将仅返回与id匹配的第一个元素,其余元素将被忽略

所以

$('.first').keyup(函数(){
updateSearch();
});
var updateSearch=函数(){
$('.second').val($('.first').val());
};
//为测试目的更改值
updateSearch()

用于唯一标识元素,因此元素的ID必须唯一

它在文档中必须是唯一的,并且通常用于检索 元素使用。id的其他常见用法包括使用 在使用设置文档样式时,该元素将被删除

当您必须对多个元素进行分组时,最简单的方法之一就是使用class属性

使用id选择器时,它将仅返回与id匹配的第一个元素,其余元素将被忽略

所以

$('.first').keyup(函数(){
updateSearch();
});
var updateSearch=函数(){
$('.second').val($('.first').val());
};
//为测试目的更改值
updateSearch()

使用下面的代码设置使用
ID
属性的字段值

$(文档).ready(函数(){
$(“[id='second']”)。每个(函数(){
$(this.val('test');
})
});

使用下面的代码设置使用
ID
属性的字段值

$(文档).ready(函数(){
$(“[id='second']”)。每个(函数(){
$(this.val('test');
})
});


元素的ID必须相同。。使用class@ArunPJohny等等,这到底起了什么作用,你能进一步解释一下这是为什么吗?这对我来说毫无意义。这有一些解释元素的ID必须相同。。使用class@ArunPJohny等等,这到底起了什么作用,你能进一步解释一下这是为什么吗?这对我来说毫无意义。这可以解释为什么“不幸”?:PNever知道这在js/jquery/html中都很重要,谢谢你帮我澄清这一点!为什么“不幸”?:PNever知道这在js/jquery/html中都很重要,谢谢你帮我澄清这一点!我想非常感谢你的投入,这是非常有价值的信息,将真正帮助我提高!我将在8分钟内接受你的回答,因为我需要等待。我想非常感谢你的投入,这是有价值的信息,将真正帮助我提高!我将在8分钟内接受你的回答,因为我必须等待。