[英]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.