[英]jquery mobile button text switching between two text via click
I have an issue with jquery mobile and a button text switch. 我在使用jquery mobile和按钮文本开关时遇到问题。
The button text should change everytime sie button is clicked. 每次单击sie按钮时,按钮文本应更改。 It is working one time only.
它仅工作一次。 I click the button, the text and backgound changes, the second time I click it nothing happens.
我单击按钮,文本和背景更改,第二次单击时,什么也没有发生。 Here ist my code:
这是我的代码:
Button declaration: 按钮声明:
<div data-role="header">
<div data-role="navbar">
<ul>
<li><a href="#" id="lock" class="lockaction" >Lock</a></li>
</ul>
</div>
</div>
And here the function I have: 这是我拥有的功能:
$(".lockaction").click(function(e) {
if (e.target.id == "lock" ) {
document.getElementById("lock").text = "Unlock";
$(this).css("background-color", "red");
}
else {
document.getElementById("lock").text = "Lock";
}
postupdate();
});
Maybe someone can help me with it. 也许有人可以帮助我。
It fails bacause your ID is always "lock". 由于您的ID始终为“锁定”,因此失败。 Check for text in element instead.
检查元素中的文本。
Try something like this: 尝试这样的事情:
$(".lockaction").click(function(e) {
e.preventDefault();
if ($(this).text() === "Lock" ) {
$(this).text("Unlock");
$(this).css("background-color", "red");
} else {
$(this).text("Lock");
$(this).css("background-color", "white");
}
postupdate();
});
You need to check what value does the text
contains. 您需要检查
text
包含什么值。 You were checking for id
, it will always return true as the id wont change, so your if
is always true. 您正在检查
id
,因为id不会更改,所以它将始终返回true,因此if
始终为true。
$(".lockaction").click(function (e) {
e.preventDefault();//prevent default action
if ($(this).text() == "Lock") {
$(this).text("Unlock");
$(this).css("background-color", "red");
} else {
$(this).text("Lock");
$(this).css("background-color", "white");
}
postupdate();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.