检查ASP.NET表单上具有特定子字符串的所有无线电控件

时间:2021-01-23 15:55:07

I'm working on a .NET-powered questionnaire which contains several repeater controls, each row of which contains three radiobuttons. I'm trying to write a simple Javascript function to check all the controls on the page which contain the word "chkGreen" in the control name/id.

我正在开发一个基于.NET的调查问卷,其中包含几个转发器控件,每行控件包含三个radiobuttons。我正在尝试编写一个简单的Javascript函数来检查页面上控件名称/ id中包含单词“chkGreen”的所有控件。

The page looks something like this:

该页面看起来像这样:

Repeater 1
Description 1           ( ) Green ( ) Yellow ( ) Red
Description 2           ( ) Green ( ) Yellow ( ) Red
Description 3           ( ) Green ( ) Yellow ( ) Red

Repeater 2
Description 1           ( ) Green ( ) Yellow ( ) Red
Description 2           ( ) Green ( ) Yellow ( ) Red
Description 3           ( ) Green ( ) Yellow ( ) Red

Here's the function so far:

这是迄今为止的功能:

   for (i = 0; i < document.Form1.elements.length; i++) {
      var _control = document.Form1.elements[i].id
      if (_control.indexOf("chkGreen") > 0) {
         eval(_control.checked = true);
      }
   }

This function does not work. When I add a document.write or alert() to the statement, it properly fires, so the logic is apparently working, it's just the actual radiobutton check code is not working.

此功能不起作用。当我在语句中添加document.write或alert()时,它会正常触发,因此逻辑显然正常工作,只是实际的radiobutton检查代码无效。

Any thoughts?

2 个解决方案

#1


You don't need to eval the _control.checked line.

您不需要评估_control.checked行。

for (i = 0; i < document.Form1.elements.length; i++) {
  var _control = document.Form1.elements[i]; // EDIT: you want the element, not the id of the element
  if (_control.indexOf("chkGreen") > 0) {
     _control.checked = true;
  }
}

#2


Thanks for the quick response. I tried the function without using eval() and it didn't work. I have since gotten the function to work by using document.getElementById:

感谢您及时的回复。我在不使用eval()的情况下尝试了这个功能,但它没有用。我已经使用document.getElementById使函数工作了:

for (i = 0; i < document.form1.elements.length; i++) {
  var _control = document.form1.elements[i].id
  if (_control.indexOf("chkGreen") > 0) {
    var thecontrol = document.getElementById(_control);
    thecontrol.checked = true;
  }
}

#1


You don't need to eval the _control.checked line.

您不需要评估_control.checked行。

for (i = 0; i < document.Form1.elements.length; i++) {
  var _control = document.Form1.elements[i]; // EDIT: you want the element, not the id of the element
  if (_control.indexOf("chkGreen") > 0) {
     _control.checked = true;
  }
}

#2


Thanks for the quick response. I tried the function without using eval() and it didn't work. I have since gotten the function to work by using document.getElementById:

感谢您及时的回复。我在不使用eval()的情况下尝试了这个功能,但它没有用。我已经使用document.getElementById使函数工作了:

for (i = 0; i < document.form1.elements.length; i++) {
  var _control = document.form1.elements[i].id
  if (_control.indexOf("chkGreen") > 0) {
    var thecontrol = document.getElementById(_control);
    thecontrol.checked = true;
  }
}