我有几个表单与同一个字段,我需要用jQuery收集set字段的值。我使用的是.each
,它适用于id="q"
的字段,但我不知道为什么,其他字段只能获得第一个值。有人可以帮助我吗?。每个字段只有一个字段
HTML:
<form name="art_1" id="art_1">
<input type="text" name="prezzol" id="prezzol">
<input type="text" name="q" id="q" onchange="ricalcola('#art_1')">
<input type="text" name="sconto" id="sconto" onchange="ricalcola('#art_1')">
</form>
<form id="art_2" name="art_2">
<input type="text" id="prezzol" name="prezzol">
<input type="text" name="q" id="q" onchange="ricalcola('#art_2')" >
<input type="text" name="sconto" id="sconto" onchange="ricalcola('#art_2')" >
</form>
<a onclick="totale()">totale</a>
的JavaScript:
function totale() {
$('#q').each(function(){
//works!! 2 value!
tquantità += parseInt($(this).val())
})
$('#sconto').each(function(){
//doesn't work, only the first value
tprezzo += $(this).val()
})
为什么你使用.each来选择一个带ID的元素??? –
使用class ID insted ID .... ID应该始终是唯一的,这就是为什么它被称为“ID”...... – bipen