简体   繁体   English

我想让jQuery使用ajax更新复选框

[英]I want to get jquery to update checked box using ajax

I am trying to get my ajax json script to update my HTML checked box with the value from my data base but cannot get it to work. 我试图让我的ajax json脚本使用数据库中的值更新HTML复选框,但无法正常工作。 Any help please. 请帮忙。

The JS is below. JS在下面。

  setInterval(function(){ $.ajax({ url:"databaseQuiry.php", dataType:"json", success: function(data){ $('niamh').prop('checked', data.niamhswitch); } }); }, 2000); 

The HTML checked box I want updated is below 我要更新的HTML复选框如下

  <div class="niamhSwitch"> <label class="switch"> <input type="checkbox" name="niamh" id="switch" value="1"> <div class="slider round"></div> </label></div> 

I Know that the json is returning niamhswitch":"1" so I have the value 1 returning from my data base, but how do I get this to make the checked box become checked for value 1 and unchecked for value 0. I have tried doing it with the element selector for class and name but both do not work. 我知道json正在返回niamhswitch“:” 1“,所以我从数据库中返回了值1,但是如何获取此值以使选中的框成为值1而未选中的值为0。使用class和name的元素选择器来完成此操作,但是两者都不起作用。

Your selector's wrong $('niamh') will look for an element whose tag name is 'niamh'. 选择器的错误$('niamh')将寻找标签名称为'niamh'的元素。

You need either $('[name=niamh]') or $('#switch') . 您需要$('[name=niamh]')$('#switch')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery将复选框的值获取到数组中 - Get checked box values into array using jquery 使用Mysql在CodeIgniter中使用Jquery获取Checked框输入值 - Get the Checked box input values using Jquery in CodeIgniter using Mysql 如何获取多个复选框的值并使用复选框中的值执行增量 - how can i get value of multiple checked box and perform increment using value from checked box 我想使用(过滤器)js来“选中”复选框 - i want to get “checked” checkboxes using (filter) js 我想使用纯JavaScript在添加表单上选中的编辑表单上选中复选框 - I want to get the checkboxes checked on edit form which i have checked on Add form using pure javascript 数组和复选框,我希望根据数组值选中/取消选中框,并在有人选中任何框并更新表单时更新数组 - array and checkboxes, I want boxes to checked/unchecked based on array values and also update array when someone check any box and update the form 我想用Jquery AciTree迭代选中的复选框 - I want to iterate the checked checkbox with Jquery AciTree 选中是否使用jquery选中复选框 - Check a check box is checked or not using jquery 使用 jquery 验证复选框列表 - To have validation for checked box list using jquery 我想使用jQuery Mobile显示iPhone的本机警报框 - I want to show native alert box for iphone using jquery mobile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM