如果checkbox发生变化,使用jquery调用php
我在页面上有一个简单的复选框,允许用户说出他们是否愿意接收电子邮件通知。我使用 jquery 在复选框更改时调用一些 php 代码。然而,我什至调用 jquery 函数(单击复选框不执行任何操作)都没有太多运气,更不用说测试后端功能了。
任何指出错误的帮助都会很棒。谢谢。
复选框 HTML:
<input id="notify_checkbox" type="checkbox" value="y" name="notify">
jquery:
$('#notify_checkbox').change(function(){
if($('#notify_checkbox').attr('checked'))
{
$.post("/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
}
else
{
$.post("/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
}
});
最后是 PHP:
function update_notify()
{
// Passed through AJAX
$notify = $_POST[checked];
$email = $_POST[email];
$this->load->model('musers');
$query = $this->musers->update_user_notify($email, $notify);
}
解决方案: 下面的评论很有帮助,但不是最终的解决方案。解决方案是在我的代码周围添加以下内容。
$(document).ready(function() {
{);
I have a simple checkbox on a page that allows a user to say if they'd like to receive email notifications. I am using jquery for this to call some php code when the checkbox changes. However, I am not having much luck even calling the jquery function (clicking the checkbox does nothing) let alone test the backend functionality.
Any help in pointing out the error would be great. Thanks.
The checkbox HTML:
<input id="notify_checkbox" type="checkbox" value="y" name="notify">
The jquery:
$('#notify_checkbox').change(function(){
if($('#notify_checkbox').attr('checked'))
{
$.post("/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
}
else
{
$.post("/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
}
});
And finally the PHP:
function update_notify()
{
// Passed through AJAX
$notify = $_POST[checked];
$email = $_POST[email];
$this->load->model('musers');
$query = $this->musers->update_user_notify($email, $notify);
}
RESOLUTION: The comments below were helpful but not the ultimate solution. The solution was to add the following around my code.
$(document).ready(function() {
{);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用
.click()
来代替呢?JSFIDDLE
另外,正如您在我的 JSFiddle 示例中看到的,使用
.is(': check')
而不是attr('checked')
。@Rocket 对您的帖子发表评论后进行编辑:
您确实应该引用您的
$_POST php 中的
值!我自己没有注意到,归功于 rocketWhy not use
.click()
instead?JSFIDDLE
Also, as you can see in my JSFiddle example, use
.is(':checked')
instead ofattr('checked')
.edit after @Rocket commented on your post:
You should indeed quote your
$_POST
values in your php! Didn't notice it myself, credits to rocket你的控制器叫什么名字?您需要将其放入 URL 中。
What's the name of your controller? You need to put that in the URL.
问题在于 jQuery 1.6 中对 attr 函数的重新定义,以及属性和属性之间的差异。
对于属性(通过
attr
检索),checked= 的值“checked”
或其缺失保持不变,无论元素是否实际被检查。使用属性(从 jQuery 1.6 开始使用
prop
检索),实际状态找到该元素。这相当于检查元素的checked
属性(这是更好的选择,因为您不需要执行新的 jQuery 选择)。最好的解决方案如下:请参阅 jsFiddles 显示此内容:
prop
this.checked
The problem is with the redefinition of the
attr
function in jQuery 1.6, and with the difference between attributes and properties.With attributes (retrieved with
attr
), the value ofchecked="checked"
or its absence stays the same, regardless of whether the element is actually checked or not.With properties (retrieved with
prop
as of jQuery 1.6), the actual state of the element is found. This is equivalent to checking thechecked
property of the element (which is preferable because you don't need to do a new jQuery selection). The best soltion would be as follows:See jsFiddles showing this:
prop
this.checked