该语法对我来说似乎是正确的,但是只有“ if”语句起作用。我什至尝试仅使用一个简单的if / else语句,并且只有“ if”语句也是如此。我究竟做错了什么?我也曾在SO上看到过类似的问题,但是他们没有回答我的问题,因为当我尝试在我的案例中使用他们的更正时,它仍然无法正常工作。
的HTML
<table>
<tr>
<td><input type="checkbox" value="test" /></td>
<td><a class="clickme" href="#">edit</a></td>
</tr>
</table>
JS
$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
console.log('Test2');
} else if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
console.log('Test');
}
});
$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
console.log('Test2');} else if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
console.log('Test');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="checkbox" value="test" /></td>
<td><a class="clickme" href="#">edit</a></td>
</tr>
</table>
你的条件是正确的结构,除了与当前值进行比较true
或false
$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked') == false) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
console.log('Test2');} else
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked') == true) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
console.log('Test');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="checkbox" value="test" /></td>
<td><a class="clickme" href="#">edit</a></td>
</tr>
</table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句