Javascript if和else部分不在查询中工作
我在jquery中尝试了if-else语句,但它不起作用Javascript if和else部分不在查询中工作,javascript,jquery,Javascript,Jquery,我在jquery中尝试了if-else语句,但它不起作用 jQuery(document).ready(function () { jQuery(".checkboxes").hide(); //toggle the componenet with class msg_body jQuery(".Select").click(function () { jQuery(this).next(".checkboxes").sli
jQuery(document).ready(function () {
jQuery(".checkboxes").hide();
//toggle the componenet with class msg_body
jQuery(".Select").click(function () {
jQuery(this).next(".checkboxes").slideToggle(500);
if ($(".select").text() == "+") {
alert('hi');
$(".select").html() == "-"
}
else {
$(".select").text() == "+";
}
return false;
});
});
任何帮助都将不胜感激。我想这是一个打字错误。。在点击事件中,将大写字母“S”替换为“S” 请尝试以下代码:-
if ($(".Select").text() == "+") {
alert('hi');
$(".Select").html("-");
}
else {
$(".Select").text() == "+";
}
或者简单地使用$(this),如下所示:-
if ($(this).text().trim() == "+") {
$(this).html("-");
} else {
$(this).text("+");
}
不能为函数调用赋值。您的代码应该抛出一个错误,如
uncaughtreferenceerror:assignment中的左侧无效
jQuery(function ($) {
$(".checkboxes").hide();
//toggle the componenet with class msg_body
$(".Select").click(function () {
$(this).next(".checkboxes").slideToggle(500);
$(this).text(function (i, txt) {
return txt.trim() == '+' ? '-' : '+'
})
return false;
});
});
看看这把小提琴:
在将代码分配为$(“.Select”).html(“-”)时,应将代码更改为该值代码>
首先,您使用的是两个不同的类。选择和。选择。我认为问题就在这里。第二件事,它应该是html('-')
而不是html()='-'
,因为它是一个逻辑/条件语句,它将返回一个布尔值。它不是赋值语句。因此,您的代码应该是:
jQuery(document).ready(function () {
jQuery(".checkboxes").hide();
//toggle the componenet with class msg_body
jQuery(".select").click(function () {
jQuery(this).next(".checkboxes").slideToggle(500);
if ($(".select").text() == "+") {
alert('hi');
$(".select").html("-");
}
else {
$(".select").text("+");
}
return false;
});
});
我认为正确的类是select
,因为您使用它的次数超过了select,请尝试$(this).text()
而不是$(.select”)。text()
请发布您的elements@soul我认为分号在英语中是不必要的jquery@RohitArora是的!!!但有时我可能不会工作太多==
条件运算符应该用作条件,它不是赋值运算符:在$(“.select”).text()中==“+”代码>。现在在你的第二次编辑中,如果分号是必需的…为什么不把它放进去?我什么时候说过分号是必需的@Manoz?阅读答案again@RohitAroraOps代码是$(“.select”).html()==“-”
,这是错误的。。。在小提琴中是$(.Select”).html(“-”代码>好了,前面的答案也是正确的。。。它使用的是OP使用的相同的if..else
,但是bug已经修复了。。。通过使用.html('+')
jQuery(document).ready(function () {
jQuery(".checkboxes").hide();
//toggle the componenet with class msg_body
jQuery(".select").click(function () {
jQuery(this).next(".checkboxes").slideToggle(500);
if ($(".select").text() == "+") {
alert('hi');
$(".select").html("-");
}
else {
$(".select").text("+");
}
return false;
});
});