我试图将选择框的文本和表单中的某些文本添加到隐藏字段中,我可以获取选择框的文本,但我可以从表单中获取所需的文本,任何人都可以帮忙。< / p>
<form>
<select name="select" id="select">
<option>Select your pizza</option>
<option value="6.65">NY, 10", £6.65</option>
<option value="8.95">NY, 12", £8.95</option>
<option value="11.95">NY, 16", £11.95</option>
<option value="3.45">Chicago, 7", £3.45</option>
<option value="6.65">Chicago, 10", £6.65</option>
<option value="8.95">Chicago, 12", £8.95</option>
<option value="11.95">Chicago, 16", £11.95</option>
<option value="19.95">Chicago, Beast 24" x 18", £19.95</option>
</select>
</form>
<form id="toppings">
<span class="descriptionsPizza">EXTRA CHEESE</span>
<input name="minus1" type="button" class="button minus" id="minus1" value=" - " />
<input name="textfield1" type="text" id="textfield1" class="valfield" size="2" maxlength="2" value="0" />
<input name="add1" type="button" class="button add" id="add1" value=" + " />
<span class="descriptionsPizza">HAM</span>
<input name="minus2" type="button" class="button minus" id="minus2" value=" - " />
<input class="valfield" name="textfield2" type="text" id="textfield2" size="2" maxlength="2" value="0"/>
<input name="add2" type="button" class="button add" id="add2" value=" + " />
</form>
$(function()
{
//functions to change the quantity field by pressing the + and - buttons
// add buttons
$(".add").click(function(){
var $this = $(this);
var quantity = parseInt($this.siblings(".valfield").val(), 10) + 1;
$(this).siblings(".valfield").val(quantity);
});
// minus buttons
$(".minus").click(function(){
var $this = $(this);
var quantity = parseInt($this.siblings(".valfield").val(), 10) - 1;
if(quantity < 0) quantity = 0;
$(this).siblings(".valfield").val(quantity);
});
$('input[name=my-add-button]').click(function() {
var randomNumber = Math.floor((Math.random() * 9000) + 100);
$('input[name=my-item-id]').val(randomNumber);
$('input[name=my-item-name]').val( $("#select :selected").text() );
$('input[name=my-item-price]').val( $('#update_price').val() );
});
所以我需要添加浇头名称(span标签中的文本),如果该浇头的数量大于0,则将跨度中的文本添加到隐藏字段以及所选文本框中。
感谢您的帮助。
答案 0 :(得分:0)
var descriptionsPizza = [];
$("input.valfield").each(function(){
if ($(this).val() > 0){
descriptionsPizza.push( $(this).prevAll(".descriptionsPizza").text() );
}
});
$("hiddenInputSelector").val(descriptionsPizza.join(' '));