【发布时间】:2014-01-10 23:07:47
【问题描述】:
按钮:
<button type="submit" id="status" class="button" value="True"><span>followed</span></button>
jQuery:
<script>
$(".button").toggle(function() {
$(this).val('followed');
}, function(){
$(this).val('follow');
});
</script>
当用户单击按钮时,我希望它切换到其他值。但是现在当我运行代码时,按钮就会从页面上消失!这段代码有什么问题?
编辑: 谢谢您的帮助。 这是整个更新的 jQuery:
<script>
$(document).ready(function(){
$(".button").click(function() {
var status = $("#status").val();
var course = $("#course").html()
//NEW SECTION that I'm having trouble with
if ($(".button span").html() == "followed") {
$(".button").on("mouseover", function () {
$(".button span").html() = "unfollow";
}}
$.ajax({
type: "POST",
url: "/follow",
data: 'status=' + status + "&course=" + course,
success: function() {
$(".button span").html($(".button span").html() == 'followed' ? 'follow' : 'followed');
$(".button").val($(".button").val() == 'True' ? 'False' : 'True');
}
});
return false;
});
});
</script>
onclick 有效。然后我添加了新的 onmouseover 代码以将“followed”更改为“unfollowed”,当我运行它时,我得到了
405 Method Not Allowed
The method POST is not allowed for this resource.
onmouseover 代码有什么问题?
还有,
的作用是什么return false;
?
【问题讨论】:
-
你需要设置点击事件。
-
@darkmango:取决于您使用的 jquery 版本。请在下面查看我的演示。您必须使用较新的版本。为此,您必须使用 1.8
标签: javascript jquery button