jquery用name获取元素并判断其值
可以使用jQuery的$("input[name='name']").val()来获取指定name属性的元素的值,并使用条件语句判断其值。以下是一个示例代码:
<!DOCTYPE html>
<html>
<head>
<title>jQuery获取元素并判断其值</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<input type="text" name="name" value="John">
<input type="text" name="age" value="25">
<button id="checkButton">Check Value</button>
<script>
$(document).ready(function() {
$("#checkButton").click(function() {
var nameValue = $("input[name='name']").val();
if (nameValue === "") {
alert("Name is empty");
} else {
alert("Name is not empty: " + nameValue);
}
});
});
</script>
</body>
</html>
在上述示例中,点击按钮后会获取名为"name"的input元素的值,并根据其值是否为空显示相应的提示框
原文地址: https://www.cveoy.top/t/topic/iONx 著作权归作者所有。请勿转载和采集!