我有幾個表單與同一個字段,我需要用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