我是javascript的新手,我有一个表单,当我尝试检索用户信息时,它无法正常工作。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<form method="post" action="">
<div class="form-row">
<label>
<span>Register</span>
<select name="register_as" id="dropdown">
<option value="none">Select One</option>
<option value="user">User</option>
<option value="designer">Designer</option>
</select>
</label>
</div>
<div>
<label><span>Gender</span></label>
<div class="form-radio-buttons">
<div>
<label>
<input type="radio" name="gender" value="male">
<span>Male</span>
</label>
</div>
<div>
<label>
<input type="radio" name="gender" value="female">
<span>Female</span>
</label>
</div>
</div>
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
<script>
$(document).ready(function() {
$("form").submit(function (e) {
//Check if gender has not been selected
var gender = $(this).find('input[name="gender"]').val();
if(!gender){
alert('select a gender');
}
//Check if 'register_as' is equal to 'user' or designer
var register_as = $("#dropdown").val();
if(register_as!= 'user' | register_as!= 'designer'){
alert('Register as user or designer');
}
e.preventDefault();
});
});
</script>
&#13;
当用户想要注册时,他必须在user
和designer
之间进行选择。如果他选择的是不同的,那么我们提醒他。
与性别相同,他必须在male
和female
之间进行选择。
请帮我解决这个问题。
答案 0 :(得分:3)
要获取无线电组值,您需要使用@Override
public void onClick(View view) {
int id = (Integer)view.getTag();
emptySlot.setImageResource(id);
}
,否则您将始终获得第一个。
:checked
布尔运算符Javascript var gender = $(this).find('input[name="gender"]:checked').val();
&&
$(document).ready(function() {
$("form").submit(function (e) {
//Check if gender has not been selected
var gender = $(this).find('input[name="gender"]:checked').val();
if(!gender){
alert('select a gender');
}
//Check if 'register_as' is equal to 'user' or designer
var register_as = $("#dropdown").val();
if(register_as!= 'user' && register_as!= 'designer'){
alert('Register as user or designer');
}
e.preventDefault();
});
});
答案 1 :(得分:1)
将.filter(':checked')
添加到性别检查中,并将|
更改为&&
到组合框检查的if条件
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<form method="post" action="">
<div class="form-row">
<label>
<span>Register</span>
<select name="register_as" id="dropdown">
<option value="none">Select One</option>
<option value="user">User</option>
<option value="designer">Designer</option>
</select>
</label>
</div>
<div>
<label><span>Gender</span></label>
<div class="form-radio-buttons">
<div>
<label>
<input type="radio" name="gender" value="male">
<span>Male</span>
</label>
</div>
<div>
<label>
<input type="radio" name="gender" value="female">
<span>Female</span>
</label>
</div>
</div>
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
<script>
$(document).ready(function() {
$("form").submit(function (e) {
//Check if gender has not been selected
var gender = $(this).find('input[name="gender"]').filter(':checked').val();
if(!gender){
alert('select a gender');
}
//Check if 'register_as' is equal to 'user' or designer
var register_as = $("#dropdown").val();
if(register_as!= 'user' && register_as!= 'designer'){
alert('Register as user or designer');
}
e.preventDefault();
});
});
</script>
&#13;