jquery表id和禁用复选框
我有一个关于在表上使用Jquery的问题 我不知道我是不是用正确的方法做的,因此在做了大量研究之后,我在这里问了我的问题 我想要的是比较变量数组中的项目,该数组保存从JSON转换的字符串响应,如果它们与表中的列表匹配,则复选框将被禁用 感谢您的任何意见。:) jQuery代码:jquery表id和禁用复选框,jquery,ajax,json,Jquery,Ajax,Json,我有一个关于在表上使用Jquery的问题 我不知道我是不是用正确的方法做的,因此在做了大量研究之后,我在这里问了我的问题 我想要的是比较变量数组中的项目,该数组保存从JSON转换的字符串响应,如果它们与表中的列表匹配,则复选框将被禁用 感谢您的任何意见。:) jQuery代码: function refreshKeywords(e) { e.preventDefault(); var refresh_form = jQuery(e.target); $.ajax({
function refreshKeywords(e) {
e.preventDefault();
var refresh_form = jQuery(e.target);
$.ajax({
url: refresh_form.attr('action'),
type: refresh_form.attr('method'),
data: refresh_form.serialize(),
dataType: 'json',
success: function(response) {
var array = $.parseJSON(response.new_list);
alert(array);
var html = '';
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
if ($('#checkKeywords :input') == array) {
$('#validate').attr('disabled', true);
}
});
$('#keywords').append(html);
if ($('#checkKeywords :input') == array) {
$('#validate').attr('disabled', true);
}
},
});
}
函数刷新关键字(e){
e、 预防默认值();
var refresh\u form=jQuery(即target);
$.ajax({
url:refresh_form.attr('action'),
类型:refresh_form.attr('method'),
数据:刷新表单.serialize(),
数据类型:“json”,
成功:功能(响应){
var数组=$.parseJSON(response.new_列表);
警报(阵列);
var html='';
$.each(数组、函数(i、项){
html+=“”+项目+“ ”;
if($('#checkKeywords:input')==数组){
$('#validate').attr('disabled',true);
}
});
$('#关键字')。附加(html);
if($('#checkKeywords:input')==数组){
$('#validate').attr('disabled',true);
}
},
});
}
下面是我的表格HTML代码:
<table id="checkKeywords">
<tbody>
{% for keyword in keyword_list %}
{% if forloop.counter|divisibleby:"3" %}<tr>{% endif %}
<td>
<input type="checkbox" id= "validate" name="cb" value="keywords" />
{{keyword.keyword_name}}
</td>
{% if forloop.counter|add:"1"|divisibleby:"3" %}</tr>{% endif %}
{% endfor %}
</tbody>
</table>
{关键字列表%中的关键字的百分比}
{%if-forloop.counter |可除数为:“3”%}{%endif%}
{{keyword.keyword_name}
{%if-forloop.counter | add:“1”| divisibleby:“3”}{%endif%}
{%endfor%}
我认为一些错误来自以下几行:
var array = $.parseJSON(response.new_list);
返回格式良好的Javascript对象,但与
if($('#checkKeywords :input') == array){...}
您正在将一组Javascript对象与格式良好的JSON解析JS对象进行比较,这些JS对象与从$('#checkKeywords:input')==array
返回的值不太兼容
如果两个操作数都是对象,则将它们作为对象进行比较,并且
只有当两者引用同一对象时,相等性测试才为真
我建议使用一些修改使这两个对象具有可比性,这些修改将获取这两个对象,并根据它们是否相等来处理它们以返回
true
或false
更新:
function refreshKeywords(e) {
e.preventDefault();
var refresh_form = jQuery(e.target);
$.ajax({
url: refresh_form.attr('action'),
type: refresh_form.attr('method'),
data: refresh_form.serialize(),
dataType: 'json',
success: function(response) {
var array = $.parseJSON(response.new_list);
alert(array);
var html = '';
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
if ($('#checkKeywords :input') == array) {
$('#validate').attr('disabled', true);
}
});
$('#keywords').append(html);
if ($('#checkKeywords :input') == array) {
$('#validate').attr('disabled', true);
}
},
});
}
在您的HTML代码中
<td>
<input type="checkbox" id="validate" name="cb"
value="{{keyword.keyword_name}}" />{{keyword.keyword_name}}
</td>
{{keyword.keyword_name}
从您的JS
...
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
theinputs = $('#checkKeywords :input')
$.each(theinputs, function(i, checkbox) {
if ($(checkbox).val() == item) {
$(checkbox).prop('disabled', true);
/* thanx charlietfl */
}
});
});
$('#keywords').append(html);
...
。。。
$.each(数组、函数(i、项){
html+=“”+项目+“ ”;
输入=$(“#检查关键字:输入”)
$.each(输入,函数(i,复选框){
if($(复选框).val()==项){
$(复选框).prop('disabled',true);
/*唐克斯·夏利特*/
}
});
});
$('#关键字')。附加(html);
...
我认为一些错误来自以下几行:
var array = $.parseJSON(response.new_list);
返回格式良好的Javascript对象,但与
if($('#checkKeywords :input') == array){...}
您正在将一组Javascript对象与格式良好的JSON解析JS对象进行比较,这些JS对象与从$('#checkKeywords:input')==array
返回的值不太兼容
如果两个操作数都是对象,则将它们作为对象进行比较,并且
只有当两者引用同一对象时,相等性测试才为真
我建议使用一些修改使这两个对象具有可比性,这些修改将获取这两个对象,并根据它们是否相等来处理它们以返回
true
或false
更新:
function refreshKeywords(e) {
e.preventDefault();
var refresh_form = jQuery(e.target);
$.ajax({
url: refresh_form.attr('action'),
type: refresh_form.attr('method'),
data: refresh_form.serialize(),
dataType: 'json',
success: function(response) {
var array = $.parseJSON(response.new_list);
alert(array);
var html = '';
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
if ($('#checkKeywords :input') == array) {
$('#validate').attr('disabled', true);
}
});
$('#keywords').append(html);
if ($('#checkKeywords :input') == array) {
$('#validate').attr('disabled', true);
}
},
});
}
在您的HTML代码中
<td>
<input type="checkbox" id="validate" name="cb"
value="{{keyword.keyword_name}}" />{{keyword.keyword_name}}
</td>
{{keyword.keyword_name}
从您的JS
...
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
theinputs = $('#checkKeywords :input')
$.each(theinputs, function(i, checkbox) {
if ($(checkbox).val() == item) {
$(checkbox).prop('disabled', true);
/* thanx charlietfl */
}
});
});
$('#keywords').append(html);
...
。。。
$.each(数组、函数(i、项){
html+=“”+项目+“ ”;
输入=$(“#检查关键字:输入”)
$.each(输入,函数(i,复选框){
if($(复选框).val()==项){
$(复选框).prop('disabled',true);
/*唐克斯·夏利特*/
}
});
});
$('#关键字')。附加(html);
...
这将有助于解决一些问题
function refreshKeywords(e) {
e.preventDefault();
var refresh_form = jQuery(e.target);
$.ajax({
url: refresh_form.attr('action'),
type: refresh_form.attr('method'),
data: refresh_form.serialize(),
dataType: 'json',
success: function(response) {
/* no need to use $.parseJSON , is handled by jQuery core*/
//var array = $.parseJSON(response.new_list);
var array = response.new_list;
var html = '';
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
/* was missing val() for input */
if ($('#checkKeywords :input').val() == /*array*/ item) {
// use prop() method() in jQuery >= 1.6
/*$('#validate').attr('disabled', true);*/
$('#validate').prop('disabled', true)
}
});
$('#keywords').append(html);
/* duplication */
/*if($('#checkKeywords :input') == array) { $('#validate').attr('disabled', true); }*/
}
});
}
函数刷新关键字(e){
e、 预防默认值();
var refresh\u form=jQuery(即target);
$.ajax({
url:refresh_form.attr('action'),
类型:refresh_form.attr('method'),
数据:刷新表单.serialize(),
数据类型:“json”,
成功:功能(响应){
/*无需使用$.parseJSON,由jQuery核心处理*/
//var数组=$.parseJSON(response.new_列表);
var数组=response.new_列表;
var html='';
$.each(数组、函数(i、项){
html+=“”+项目+“ ”;
/*输入缺少val()*/
if($('#checkKeywords:input').val()=/*数组*/项){
//在jQuery>=1.6中使用prop()方法()
/*$('#validate').attr('disabled',true)*/
$('#validate').prop('disabled',true)
}
});
$('#关键字')。附加(html);
/*复制品*/
/*if($('#checkKeywords:input')==数组){$('#validate').attr('disabled',true);}*/
}
});
}
这将有助于解决一些问题
function refreshKeywords(e) {
e.preventDefault();
var refresh_form = jQuery(e.target);
$.ajax({
url: refresh_form.attr('action'),
type: refresh_form.attr('method'),
data: refresh_form.serialize(),
dataType: 'json',
success: function(response) {
/* no need to use $.parseJSON , is handled by jQuery core*/
//var array = $.parseJSON(response.new_list);
var array = response.new_list;
var html = '';
$.each(array, function(i, item) {
html += "<li>" + item + "</li>";
/* was missing val() for input */
if ($('#checkKeywords :input').val() == /*array*/ item) {
// use prop() method() in jQuery >= 1.6
/*$('#validate').attr('disabled', true);*/
$('#validate').prop('disabled', true)
}
});
$('#keywords').append(html);
/* duplication */
/*if($('#checkKeywords :input') == array) { $('#validate').attr('disabled', true); }*/
}
});
}
函数刷新关键字(e){
e、 预防默认值();
var refresh\u form=jQuery(即target);
$.ajax({
url:refresh_form.attr('action'),
类型:refresh_form.attr('method'),
数据:刷新表单.serialize(),
数据类型:“json”,
成功:功能(响应){
/*无需使用$.parseJSON,由jQuery核心处理*/
//var数组=$.parseJSON(response.new_列表);
var数组=response.new_列表;
var html='';
$.each(数组、函数(i、项){
html+=“”+项目+“ ”;
/*输入缺少val()*/
如果($(')#检查关键字:i