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

时间:2021-07-12 00:10:44

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. Any help please.

我试图让我的ajax json脚本用我的数据库中的值更新我的HTML复选框,但无法让它工作。请帮忙。

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.我试过了使用类和名称的元素选择器来执行它,但两者都不起作用。

1 个解决方案

#1


2  

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')。

#1


2  

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')。