嗨,我想显示基于所选国家/地区选项的付款方式。我有以下代码。
国家选择
$countries = json_decode(file_get_contents(resource_path('views/partials/country.json')));
<select name="country" id="countriess" class="form-control">
<option value="nil"> -- select an option -- </option>
@foreach($countries as $key => $country)
<option data-mobile_code="{{ $country->dial_code }}" value="{{ $country->country }}" data-code="{{ $key }}">{{ __($country->country) }}</option>
@endforeach
</select>
加载网关
@foreach($gatewayCurrency as $data) // $data->country has country name from database
@endforeach
我已经尝试过使用jquery
<script type="text/javascript">
$('#countriess').on('change',function(){
if("{{$data->country}}"!=$(this).val()){
$("#country_{{$data->country}}").hide();
}
else if("{{$data->country}}"== $(this).val()){
$("#country_{{$data->country}}").show();
}
else if("{{$data->country}}"!=$(this).val()){
$("#country_{{$data->country}}").hide();
}
});
</script>
但问题是,如果其他国家的网关在数据库中排名倒数第二,那么它们也会显示出来
感谢您的提问。以下是答案:
- 首先在payment-methods元素中添加
payment_methods
类 - 然后添加以下javascript代码
$('#countries').on('change', function(){
let countryId = $(this).val() // get selected country id
$('.payment_methods').hide(); // hide the all payment methods
$("#country_" + countryId).show(); // then show payment methods for selected country id
});
谢谢!