我具有调用json
值的功能
现在,当用户打开帐户编辑页面时,我需要在页面打开时默认选择国家/地区
var url_country="<?=base_url()?>country.json";
var negara_list ="<option value=''></option>";
$.getJSON(url_country, function(data) {
$.each(data,function(key,val){
negara_list += "<option value="+val+">"+val+"</option>";
});
$(".negara").html(negara_list);
});
答案 0 :(得分:2)
尝试一下,希望对您有帮助
var url_country="<?=base_url()?>country.json";
var user_define_country = 'USA';
var negara_list ="<option value=''></option>";
$.getJSON(url_country, function(data) {
$.each(data,function(key,val){
if(user_define_country == val){
negara_list += "<option value="+val+" selected='selected'>"+val+"</option>";
}else{
negara_list += "<option value="+val+">"+val+"</option>";
}
});
$(".negara").html(negara_list);
});
答案 1 :(得分:0)
您可以尝试类似的操作,如果您的json响应仅是一个国家/地区,则无需播放@Slf4j
@CrossOrigin(origins = "*")
@RestController
public class ComplainsController {
@CrossOrigin(origins = "*")
@RequestMapping(value = "/aalcomplaint/api/complaint/lodge-complaint", method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView lodgeComplaint(final HttpServletRequest request, ModelAndView modelAndView, final RedirectAttributes redirectAttributes) {
modelAndView.addObject("policyNumber","12345");
modelAndView.setViewName("redirect:/list-complains");
return modelAndView;
}
}
函数,如果您的响应不只一个国家/地区,则可以指定将哪个国家/地区设为默认国家/地区< / p>
针对单个国家/地区的答复
each
用于与多个国家/地区进行回复
var url_country="<?=base_url()?>country.json";
var negara_list ="<option value=''></option>";
negara_list += "<option value="+url_country.country+" selected>"+url_country.country+"</option>";
$(".negara").html(negara_list);
});