我的注册表有问题。我使用foreach
来获得所有带有selects和extra选项的附加功能,但是我想确保如果选中1复选框,则必须进行选择。现在我所拥有的是,如果您选中1复选框并额外增加1,则所有选择都将变为必需。
所以我想要的是:在jquery中将extraoptions_id
类与.extraoptions
类一起提供,这样它仅使id成为必需的选择,而不是全部。
注册表格html
<label>Options</label><br>
@foreach($option_array as $option)
<div>
<input type="checkbox" class="option" id="option_{{ $option->exa_id }}" name="option_{{ $option->exa_id }}" value="{{ $option->exa_id }}" {{ isset($cache) ? (isset($cache['option_' . $option->exa_id]) ? 'checked' : '') : (old() ? (old('option_' . $option->exa_id) ? 'checked' : '') : ($registration ? (in_array($option->exa_id,$registration_options) ? 'checked' : '') : '')) }} >
<input type="hidden" value="{{ $option->exa_price }}" class="option_price_{{ $option->exa_id }}">
<label>{{ $option->exa_name }}</label> <label class="exa_price"> €{{ $option->exa_price }} </label>
</div>
<select name="extraoptions_{{ $option->exa_id }}" class="form-control extraoptions" id="extraoptions_{{ $option->exa_id }}">
<option></option>
@foreach($option->extraoptions as $extraoption)
<option value="{{ $extraoption->eos_id }}" {{ ($registration? (in_array($extraoption->eos_id,$registration_options_extra) ? 'selected' : '') : '') }}>{{ $extraoption->eos_name }}</option>
@endforeach
</select>
<br>
@endforeach
jQuery
$(".option").change(function(){
var current_price = parseFloat($(".price").val());
//console.log(current_price);
var id = $(this).attr('id');
console.log(id);
var id_number = id.split("_")[1];
//console.log(id_number);
var option_price = parseFloat($(".option_price_" + id_number).val());
//console.log(option_price);
if(this.checked){
var new_price = parseFloat(current_price + option_price).toFixed(2);
$(".price").val(new_price);
var extraoption_id = $(".extraoptions").attr('id');
console.log(extraoption_id);
//the problem:
var extraoption_number = extraoption_id.split("_")[1];
console.log(extraoption_number);
$(".extraoptions").attr('required','True');
}
else{
var new_price = parseFloat(current_price - option_price).toFixed(2);
$(".price").val(new_price);
$(".extraoptions").attr('required','False');
}
}
那我该如何给extraoptions_id
加上必需的?预先感谢!