一、JS中获取值和赋值。
document.getElementById('password').value;
var integral = document.getElementById('jifen').innerText;
var productId1 = $("#biaoqian").val();
var id= '${id}';
var id= [[${id}]];(thymeleaf)
document.getElementById("password").innerHTML = "密码错误";
var obj = document.getElementById("btnB");
obj.style.backgroundColor = "#dddcdc";
var obj = document.getElementById("btn3");
obj.className = "little_circle active";
二、点击事件
1.通过class添加点击事件。
$(function(){
$(".aga").click(function(){
alert();
});
});
2.通过id添加点击事件。
$('#body').on('click','#agediv3',function() {
alert();
});
三、thymeleaf
th:each="list:${list}"
th:value="${list.jobNumber}"
th:value="${#dates.format(shopLottery.lotterTimeEnd,'yyyy-MM-dd HH:mm:ss')}"
th:text="${list.nickName}"
th:if="${list1.examineStatus} eq 0"
th:if="${list.status eq '待审核' || list.status eq '匹配成功待审核'}"
gt:great than(大于)>
ge:great equal(大于等于)>=
eq:equal(等于)==
lt:less than(小于)<
le:less equal(小于等于)<=
ne:not equal(不等于)!=
th:onclick="selectList([[${list.id}]])"
th:onclick="selectList([[${list}]])"
function selectList(id) {
}
th:object="${shopLottery}"
th:field="*{lotteryId}"
字典类型:
<div class="col-sm-8" th:with="type=${@dict.getType('staff_status')}">
<label th:each="dict : ${type}" class="radio-box">
<input type="radio" name="status"
th:checked="${wxAssociateStaff.status} == ${dict.dictValue} ? true : false"
th:text="${dict.dictLabel}" th:value="${dict.dictValue}"/>
</label>
</div>
下拉框
<option th:each="list:${list}" th:value="${list.jobNumber}" th:text="${list.nickName}">下拉分类一</option>
var options=$("#jobSel option:selected");
options.val();
<select id="idState" style="width: 150px" name="idState" th:value ="${stateval}" >
<option value="1" th:selected="${stateval == 1}">状态一</option>
<option value="2" th:selected="${stateval == 2}">状态二</option>
<option value="3" th:selected="${stateval == 3}">状态三</option>
<option value="4" th:selected="${stateval == 4}">状态四</option>
</select>
js:
var myType = document.getElementById("idState");
var index = myType.selectedIndex;
var stateval = myType.options[index].value;
myType.options[index].text;
th:selected="${stateval == 4}"
四、前后端传值
1.往后端传普通数据。数据和业务在后端处理完再回去前端再进行跳转。
function deleteorder(id) {
$.ajax({
url: 后端方法地址,
data: {"id": id, "key": value,"key":value},
type: 'post',
dataType: "json",
success: function (data) {
if (data.success == true) {
window.location.href = 需要跳转页面的地址;
}else{
}
}
});
}
@PostMapping("/auditList")
@ResponseBody
public AjaxJson auditList(HttpServletRequest request){
AjaxJson j = new AjaxJson();
String id= request.getParameter("id");
String value= request.getParameter("key");
Integer valueInt= Integer.parseInt(request.getParameter("keyInt"));
j.setMsg("成功");
j.setSuccess(true);
return j;
j.setMsg("失败");
j.setSuccess(false);
return j;
}
2.往后端传值,直接跳转进入下一个页面。map里面的值可以在return的页面里面直接引用进行渲染。
function setpass() {
window.location.href = ctx + "CenterController/setpass?id="+id+"&key=" + value;
}
@RequestMapping("/setpass")
public String setpass(String id, String key, ModelMap map ){
map.put("list",list);
return "item";
}
3.往controller传list对象
js:
function selectList(list) {
$.ajax({
url: ctx + "LogoController/auditList",
data: JSON.stringify(list),
type: 'post',
dataType: "json",
headers:{ 'Content-Type':'application/json;charset=UTF-8' },
success: function (data) {
if (data.success == true) {
window.location.href =
}
}
});
}
controller:
@PostMapping("/auditList")
@ResponseBody
public AjaxJson auditList(@RequestBody List<demo> list){}
4.JSON对象和JSON字符串的区别
JSON字符串:var str = "{name:'xmt',sex:'woman'}";
JSON对象:var str = {name:'xmt',sex:'woman'};
JSON字符串转化为JSON对象:var obj = JSON.parse(str);
JSON对象转化为JSON字符串:var str = JSON.stringify(obj);
var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"};
data: JSON.stringify(jsonObj),