如何动态设置复选框的已检查属性 [英] how to set checked attribute of a checkbox dynamically
问题描述
Hello Friends,
我正在使用MVC4创建一个视图,我必须根据从数据库接收的记录数动态创建控件。我有两种类型的控件文本框和复选框。文本框值我动态设置我得到的模型。但是在设置复选框的选中属性时我卡住了。我想要的是,如果我收到feature_flag值,那么对于该行复选框应该被选中,否则取消选中。
下面是我写的代码: -
函数OnSuccessSelectedModuleFeatureList(数据){
var appdata = data.applicationAdmin;
var row =;
调试器;
$ .each(appdata,function(index,item)
{
row + =
+ item.feature_name + | < input type = \checkbox \id = \检查+ index +\> | < input type = \ text \value =+ item.feature_value +> |
if(item.feature_flag == Y)
{
var a ='检查'+索引;
$('#Check'+ index).attr('选中','选中');
// $('#Check'+ index).prop('checked',true);
}
其他
{
$('#Check'+ index).prop('checked',false);
}
});
$('#FlagAutomationModuleSelectedModuleFetures tbody')。html(row);
$('#SelectedModuleNameStep2')。html(SelectedModuleName)
}
对于它正在工作的文本框但是对于复选框它不起作用。
如果有人可以提供帮助,我将不胜感激。如果需要更多细节,请回复。
提前致谢。
Hello Friends,
I am creating a view using MVC4 where I have to create controls dynamically based on the number of records I am receiving from database. I have two type of controls textbox and checkbox. Textbox value I am setting dynamically whatever I am gettingin my model. But I am stuck while setting checked attribute of checkbox. What I want is if I received feature_flag value Yes then for that row checkbox should be checked else uncheck.
Below is the code I have written:-
function OnSuccessSelectedModuleFeatureList(data) {
var appdata = data.applicationAdmin;
var row = "";
debugger;
$.each(appdata, function (index, item)
{
row += "
" + item.feature_name + " | <input type=\"checkbox\" id=\"Check" + index + "\"> | <input type=\"text\" value=" + item.feature_value + "> |
if (item.feature_flag == "Y")
{
var a = 'Check' + index;
$('#Check' + index).attr('checked', 'checked');
// $('#Check' + index).prop('checked', true);
}
else
{
$('#Check' + index).prop('checked', false);
}
});
$('#FlagAutomationModuleSelectedModuleFetures tbody').html(row);
$('#SelectedModuleNameStep2').html(SelectedModuleName)
}
for textboxes it is working but for checkboxes it is not working.
I would be grateful if anybody can help. Please reply if more details are required.
Thanks in advance.
推荐答案
.each(appdata,功能(索引,项目)
{
row + =;
+ item.feature_name + < input type = \checkbox \id = \检查+ index +\> < input type = \text \ value =+ item.feature_value +>
if(item.feature_flag ==Y)
{
var a ='检查'+索引;
.each(appdata, function (index, item)
{
row += "";
" + item.feature_name + " <input type=\"checkbox\" id=\"Check" + index + "\"> <input type=\"text\" value=" + item.feature_value + ">
if (item.feature_flag == "Y")
{
var a = 'Check' + index;
('#Check'+ index).attr( 'checked','checked');
//
('#Check' + index).attr('checked', 'checked');
//
('#Check'+ index).prop('checked',true);
}
其他
{
('#Check' + index).prop('checked', true);
}
else
{
这篇关于如何动态设置复选框的已检查属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!