If和else部分在查询中不工作
if and else part not working in query
我尝试了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").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("+");
}
不能给函数调用赋值。您的代码应该抛出类似Uncaught ReferenceError: Invalid left-hand side in 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
和.select
。我认为问题就在这里。第二件事,它应该是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
更多。
查看这个小提琴:JsFiddle你应该把你的代码改成这个,同时把它赋值为$(".Select").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;
});
});
相关文章:
- JSON:查询联接表不工作(Cordova Mobile)
- 无法使我的文本参数与我的查询一起工作
- 如何正确创建带有“Url.Action”的查询字符串的网址,以便它在Firefox中工作
- 媒体查询打印无法通过 javascript 命令工作
- 忽略服务工作进程请求中的查询参数
- j查询事件已记录.它究竟是如何工作的
- j查询下拉菜单.页面标记无法正常工作 - 或者我想要它的方式
- j查询每个函数无法正常工作
- AJAX 查询工作 50+ 次,然后崩溃 JavaScript
- j查询同位素错误.停止在 Wordpress 中工作
- 通过输入键(不起作用)或搜索按钮(工作)查询结果
- j查询验证规则无法正常工作
- j查询延迟无法正常工作
- 提前输入.js:页脚查询如何工作
- @media查询以奇怪的方式工作
- 用于将值从复选框传递到查询字符串的 JavaScript 无法正常工作
- j查询轮播/图库无法正常工作
- 如何在使用新列升级表模式时使SELECT查询工作
- 不能让我的地理空间查询工作.在Node中查询Mongo
- PHP脚本不返回查询,但MySql查询工作