我正在尝试单击输入类型复选框。单击时,将执行AJAX调用。我设置了一个监听器,但是什么都没有触发...firebug也不显示任何内容。
代码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js{API removed}"></script>
<script type="text/javascript">
$(document).ready(function() {
alert('test');
if ($('#profile_visible:checked').val() !== null) {
$.ajax({
url: 'inc/profileVisible.php',
success: function(data) {
$('#resultProfileVisible').innerhtml="success";
alert('Load was performed.');
}
});
}
}
</script>
文档正文中的...and:
<form method="post" action="profile/<?php echo $_SESSION['usern']; ?>/settings">
<p><input type="checkbox" id="profile_visible" name="profile_visible" /> Show Profile<span id="resultProfileVisible"></span></p>
</form>
谢谢
发布于 2012-01-28 17:45:56
您没有设置事件侦听器。使用on
(jQuery > 1.7)或bind
(jQuery < 1.7)或jQuery的各种快捷方法(如.change
或.click
)绑定事件处理程序:
$(document).ready(function() {
$("#profile_visible").change(function() {
if (this.checked) {
$.ajax({
url: 'inc/profileVisible.php',
success: function(data) {
$('#resultProfileVisible').html("success");
alert('Load was performed.');
}
});
}
});
});
此外,在使用jQuery对象时,使用.html()
而不是.innerHtml
来设置元素的html内容。
发布于 2012-01-28 17:46:05
惟一的监听器是准备好DOM的,所以它只会运行一次。
您的代码有一些问题。见下文。
$(document).ready(function () {
alert('test');
// I assume you just want to see if it is checked
if ($('#profile_visible').is(':checked')) {
$.ajax({
url: 'inc/profileVisible.php',
success: function (data) {
// jQuery has .html(), not innerhtml
$('#resultProfileVisible').html( "success" );
alert('Load was performed.');
}
});
}
}) // <-- was missing closing parentheses
发布于 2012-01-28 17:46:07
如下所示更改代码
$(document).ready(function(){
$('#profile_visible').change(function(){ // handler when the checkbox is toggled
if($(this).attr('checked')) { // check if it is checked
$.ajax({
url: 'inc/profileVisible.php',
success: function(data) {
$('#resultProfileVisible').html("success"); // change here
alert('Load was performed.');
}
});
}
});
});
https://stackoverflow.com/questions/9047413
复制相似问题