本文主要是介绍Ajax中根据json数据不同,对页面上的单选框Radio进行回显,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Ajax中根据json数据不同,对页面上的单选框Radio进行回显
js代码:
$(document).ready(function(){$.ajax({type: "POST",url: path+"/pop/nowTodayMeet2",dataType: "json",success: function(data){$("#discussTopicsEdit").val(data.distopics);$("#metOp_Date").val(data.metdate);$("#verdict").val(data.verdict);$("#reason").val(data.reason);$("input[name=part][value="+data.part+"]").attr("checked",true);$("input[name=nopartreason][value="+data.nopartreason+"]").attr("checked",true);//$("input[name='part'][value='data.part']").attr("checked",true);//$("input[name='part']").attr("checked",'0');//$("input[type=radio]").attr("checked",'0');//$("input[name='nopartreason'][value='data.nopartreason']").attr("checked",true);$("#nopartreason2").val(data.nopartreason2);}});
});
Java代码:
@RequestMapping(value = "/nowTodayMeet2")public @ResponseBody String nowTodayMeet2(HttpServletRequest request, HttpServletResponse response) {ResMessage message = ResMessageFactory.getDefaultInstance(request);try {String dateStr = com.yuanls._comm.util.Utils.getFormatDate("yyyy-MM-dd");String sql = "select to_char(ddatetime,'yyyy-mm-dd') ddatetime,con from T_FWORKMEETSUB where ddatetime=to_date(?,'yyyy-mm-dd')";List<Object> dataList = new ArrayList<Object>();dataList.add(dateStr);EntityManager entityManager = dao.getEntityManager();List<Map<String, Object>> list = ybzxTwoService.queryListMapByList(sql, dataList, entityManager);List<Map<String, Object>> list2 = null ;//得到会商人员的今天历史记录T_subject 开始LUser user = (LUser) request.getSession().getAttribute("user");if (user !=null) {String sql2 = "select verdict,reason,part,nopartreason from T_SUBJECT where ddatetime=to_date(?,'yyyy-mm-dd') and mman=?";List<Object> dataList2 = new ArrayList<Object>();dataList2.add(dateStr);dataList2.add(user.getCname().toString().trim());list2 = ybzxTwoService.queryListMapByList(sql2, dataList2, entityManager);}//结束net.sf.json.JSONObject jsonObject = new JSONObject();jsonObject.put("metdate", com.yuanls._comm.util.Utils.getFormatDate("yyyy-MM-dd"));if (list != null && list.size() > 0) {Map<String, Object> map = list.get(0);String distopics = map.get("con".toUpperCase()) + "";jsonObject.put("distopics", distopics);}if(list2 != null && list2.size() > 0) {Map<String, Object> map = list2.get(0);String verdict = map.get("verdict".toUpperCase()) + "";String reason = map.get("reason".toUpperCase()) + "";String part = map.get("part".toUpperCase()) + "";String nopartreason = map.get("nopartreason".toUpperCase()) + "";String nopartreason2 = "";if(nopartreason.length()>=2) {nopartreason = nopartreason.toString().trim();String nopart = nopartreason.substring(0, 2);if("补休".equals(nopart)) {nopartreason2 = nopartreason.substring(2);nopartreason = "1";}else if("开会".equals(nopart)) {nopartreason2 = nopartreason.substring(2);nopartreason = "2";}else if("出差".equals(nopart)) {nopartreason2 = nopartreason.substring(2);nopartreason = "3";}else {nopartreason2 = nopartreason;nopartreason = "";}}else {nopartreason2 = nopartreason;nopartreason = "";}jsonObject.put("verdict", verdict);jsonObject.put("reason", reason);jsonObject.put("part", part);jsonObject.put("nopartreason", nopartreason);jsonObject.put("nopartreason2", nopartreason2);}this.setSuccess(message);return jsonObject.toString();} catch (Exception e) {log.error(e.getMessage(), e);this.setError(this.getClass(), message, e.getMessage(), request);}return message.getString();}
JSP代码:
<div class="span8" style="width:650px;height:50px;"><label class="control-label" style="width:100px">是否参加:</label><div class="controls control-row4"><label><input name="part" id="part" type="radio" value="1" style="width:20px"/>参加</label> <label><input name="part" id="part" type="radio" value="0" style="width:20px"/>不参加</label> </div>
</div><div class="span8" style="width:650px;height:50px;"><label class="control-label" style="width:100px">不参加理由:</label><div class="controls control-row4"><label><input name="nopartreason" id="nopartreason" type="radio" value="1" style="width:20px"/>补休</label> <label><input name="nopartreason" id="nopartreason" type="radio" value="2" style="width:20px"/>开会</label> <label><input name="nopartreason" id="nopartreason" type="radio" value="3" style="width:20px"/>出差</label> <label>其它:<input id="nopartreason2" type="text" style="font-size: 14px;width:246px;"></label></div>
</div>
单选框的回显:
$("input[name=part][value="+data.part+"]").attr("checked",true);
$("input[name=nopartreason][value="+data.nopartreason+"]").attr("checked",true);
这篇关于Ajax中根据json数据不同,对页面上的单选框Radio进行回显的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!