【发布时间】:2019-08-01 07:05:21
【问题描述】:
- 我有两个复选框。
- 第一个已检查。
- 如果复选框被选中,则显示输入字段(它正在工作)。
- 当我选中另一个复选框时,第一个复选框应自动取消选中(工作)。
- 问题是当第一个复选框未选中时,第一个复选框生成的输入字段不会消失。
谁能指导我做错了什么?
$('#Experience').click(function(){
if($(this).is(':checked')){
var tb = $('<input type="number" name="totalyr" min="0" id="common" placeholder="Total Year Experience" class="form-control col-xs-3" required><input type="number" name="totalmn" min="0" id="common" class="form-control col-xs-3" placeholder="Total Month Experience" required><br><input type="text" name="currentjob" id="common" class="form-control col-xs-3" placeholder="Current Job Title" required><input type="text" name="currentjob" id="common" class="form-control col-xs-3" placeholder="Current Company Name" required><br><input type="number" id="common" name="rupee" min="0" class="form-control col-xs-3 fa fa-rupee" placeholder="Annual Salary ₹:" required>');
$(this).after(tb) ;
}
else if($(this).siblings('#common').length>0){
$(this).siblings('#common').remove();
}
})
/*Here for second field*/
$('#Fresher').click(function(){
if($(this).is(':checked')){
var tb = $('<input type="text" name="lookingfor" min="0" id="common" placeholder="Currently Looking For" class="form-control col-xs-3" required>');
$(this).after(tb) ;
}
else if($(this).siblings('#common').length>0){
$(this).siblings('#common').remove();
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<table>
<tr>
<td>
<input type="checkbox" class="radio" value="1" id="Fresher" name="" checked />
<input type="checkbox" class="radio" value="1" id="Experience" name="" />
</td>
</tr>
</table>
在这里你可以看到,如果我选择第二个复选框,那么第一个复选框生成的输入字段不会消失
【问题讨论】:
-
您的 sn-p 工作与您的问题描述不匹配。
-
如果一个复选框被选中然后另一个不会自动取消选中,请检查那里。
标签: javascript jquery html checkbox