Serialize()在感兴趣的div之前找到所有div

时间:2015-09-25 20:35:49

标签: javascript jquery html css javascriptserializer

我有这段代码

    <body>
    <form id="generico">
    <input type="hidden" value="50.00" name="credito_residuo" id="credito_residuo">

    <div class="adding">
    <div style="float:left; margin-right:3%">Pirulo</div>
    <div><input type="hidden" name="prodotto[]" class="prodotto" value="Pirulo"></div>
    <div style="float:left; margin-right:3%">&euro; 1.00</div>
    <div><input type="hidden" name="prezzo[]" class="price" value="1.00"></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="2" name="quantita[]" class="quantita" id="quantita" ></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="7" name="totale_parziale[]" class="totale_parziale">
    </div>
    <div style="margin-right:3%; float:left">
    <input type="button" name="add" class="add" value="+"></div>
    <div style="margin-right:3%; float:left">
    <input type="button" disabled name="remove" class="remove" value="-"></div>
    <div style="margin-right:3%">
    <input type="button" disabled name="addtocart" class="addtocart" value="addtocart"></div>
    </div>

    <div class="adding">
    <div style="float:left; margin-right:3%">Maxibon</div>
    <div><input type="hidden" name="prodotto[]" class="prodotto" value="Maxibon"> </div>
    <div style="float:left; margin-right:3%">&euro; 2.00</div>
    <div><input type="hidden" name="prezzo[]" class="price" value="2.00"></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="2" name="quantita[]" class="quantita" id="quantita" ></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="7" name="totale_parziale[]" class="totale_parziale">    </div>
    <div style="margin-right:3%; float:left">
    <input type="button" name="add" class="add" value="+"></div>
    <div style="margin-right:3%; float:left">
    <input type="button" disabled name="remove" class="remove" value="-"></div>
    <div style="margin-right:3%">
    <input type="button" disabled name="addtocart" class="addtocart" value="addtocart"></div>
    </div>

    <div class="adding">
    <div style="float:left; margin-right:3%">Limonata</div>
    <div><input type="hidden" name="prodotto[]" class="prodotto" value="Limonata"></div>
    <div style="float:left; margin-right:3%">&euro; 1.50</div>
    <div><input type="hidden" name="prezzo[]" class="price" value="1.50"></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="2" name="quantita[]" class="quantita" id="quantita" ></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="7" name="totale_parziale[]" class="totale_parziale"></div>
    <div style="margin-right:3%; float:left">
    <input type="button" name="add" class="add" value="+"></div>
    <div style="margin-right:3%; float:left">
    <input type="button" disabled name="remove" class="remove" value="-"></div>
<div style="margin-right:3%">
    <input type="button" disabled name="addtocart" class="addtocart" value="addtocart"></div>
    </div>

    <div class="adding">
    <div style="float:left; margin-right:3%">Coca Cola</div>
<div><input type="hidden" name="prodotto[]" class="prodotto" value="Coca Cola" >   </div>
    <div style="float:left; margin-right:3%">&euro; 1.50</div>
    <div><input type="hidden" name="prezzo[]" class="price" value="1.50"></div> 
    <div style="float:left; margin-right:3%">
    <input type="text" size="2" name="quantita[]" class="quantita" id="quantita" ></div>
    <div style="float:left; margin-right:3%">
    <input type="text" size="7" name="totale_parziale[]" class="totale_parziale"> </div>
    <div style="margin-right:3%; float:left">
    <input type="button" name="add" class="add" value="+"></div>
    <div style="margin-right:3%; float:left">
    <input type="button" disabled name="remove" class="remove" value="-"></div>
    <div style="margin-right:3%">
    <input type="button" disabled name="addtocart" class="addtocart" value="addtocart"></div>
    </div>
    <div id="totale"></div>
    <div id="result"></div>
    <script src="js/mine.js"></script>
    <input type="submit" value="Ordina" name="completa_ordine" id="completa_ordine"></div></div>'
    </form>
  </body>

我尝试使用jQuery serialize()在div中使用ADDING类获取一些数据。我只需要字段prodotto[]prezzo[]quantita[]totale_residuo[]

我试过这段代码:

$(document).on("click", ".addtocart", function(){

  alert($(this).parents().serialize());

});

不幸的是,这会选择所有字段。

你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作。

$(this).parent().parent().find('input').serialize()