通过传递参数来捕获javascript函数中的控件ID

时间:2019-04-03 19:27:05

标签: javascript asp.net

嗨,所以我的JS技巧有些生锈,但是我正在尝试创建一些函数,以便它们在客户端运行,而不是运行服务器代码,这是我想做的事情:

我有一个下拉控件和一个文本框。文本框取决于下拉列表中的值,并且在页面加载时被禁用。

我正在尝试调用一个函数,因此,如果从下拉列表中选择“是”,则将为文本输入启用文本框。这是我在做什么:

function DependentControlByDropDown(sender, Control, DependentControlID) {
    try {

       var senderControl = sender.selectedOptions[0].text;
       var controlEnable = (senderControl == Control);
       console.log(controlEnable);

       var control = document.getElementById(DependentControlID); this gives me NULL if i do console.log(control)
       DependentControlID.disabled = controlEnable; //controlEnable has a value of either TRUE or FALSE but it doesn't work on DependentControlID
       }
   catch (e) {
        alert("DependentControlByDropDown : " + e.message);
   }
  }

我在下拉菜单中这样称呼它:

onchange="DependentControlByDropDown(this,'Yes','txtbox1')"

因此,这里发生的是我使用console.log测试了正确的值“是”或“否”。问题在于依赖控件。如果我执行console.log(DependentControlID),它会向我显示正确的控件,但是当我尝试disabled=false时,它不起作用。我也尝试过document.getElementByID(DependentControlID).disabled=false,但这也不起作用。有帮助吗?!

2 个答案:

答案 0 :(得分:1)

问题是您试图通过文本ID来访问文本框。您需要在其节点值上设置class CenterViewFlowLayout: UICollectionViewFlowLayout { override var collectionViewContentSize: CGSize { // Only support single section for now. // Only support Horizontal scroll let count = self.collectionView?.dataSource?.collectionView(self.collectionView!, numberOfItemsInSection: 0) let canvasSize = self.collectionView!.frame.size var contentSize = canvasSize if self.scrollDirection == UICollectionViewScrollDirection.horizontal { let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1) let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1) let page = ceilf(Float(count!) / Float(rowCount * columnCount)) contentSize.width = CGFloat(page) * canvasSize.width } return contentSize } func frameForItemAtIndexPath(indexPath: NSIndexPath) -> CGRect { let canvasSize = self.collectionView!.frame.size let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1) let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1) let pageMarginX = (canvasSize.width - CGFloat(columnCount) * self.itemSize.width - (columnCount > 1 ? CGFloat(columnCount - 1) * self.minimumLineSpacing : 0)) / 2.0 let pageMarginY = (canvasSize.height - CGFloat(rowCount) * self.itemSize.height - (rowCount > 1 ? CGFloat(rowCount - 1) * self.minimumInteritemSpacing : 0)) / 2.0 let page = Int(CGFloat(indexPath.row) / CGFloat(rowCount * columnCount)) let remainder = Int(CGFloat(indexPath.row) - CGFloat(page) * CGFloat(rowCount * columnCount)) let row = Int(CGFloat(remainder) / CGFloat(columnCount)) let column = Int(CGFloat(remainder) - CGFloat(row) * CGFloat(columnCount)) var cellFrame = CGRect.zero cellFrame.origin.x = pageMarginX + CGFloat(column) * (self.itemSize.width + self.minimumLineSpacing) cellFrame.origin.y = pageMarginY + CGFloat(row) * (self.itemSize.height + self.minimumInteritemSpacing) cellFrame.size.width = self.itemSize.width cellFrame.size.height = self.itemSize.height if self.scrollDirection == UICollectionViewScrollDirection.horizontal { cellFrame.origin.x += CGFloat(page) * canvasSize.width } return cellFrame } override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { guard let attr = super.layoutAttributesForItem(at: indexPath as IndexPath)?.copy() as? UICollectionViewLayoutAttributes? else { return nil } attr!.frame = self.frameForItemAtIndexPath(indexPath: indexPath as NSIndexPath) return attr } override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let originAttrs = super.layoutAttributesForElements(in: rect) var attrs: [UICollectionViewLayoutAttributes]? = Array<UICollectionViewLayoutAttributes>() for attr in originAttrs! { let idxPath = attr.indexPath let itemFrame = self.frameForItemAtIndexPath(indexPath: idxPath as NSIndexPath) if itemFrame.intersects(rect) { let nAttr = self.layoutAttributesForItem(at: idxPath) attrs?.append(nAttr!) } } return attrs } } 属性,即disabled

此外,您尝试将文本框的Disabled属性设置为与control相同的值。我已将其设置为controlEnable

OPPOSITE

controlEnable
function DependentControlByDropDown(sender, Control, DependentControlID) {
  try {
var senderControl = sender.selectedOptions[0].text;
var controlEnable = (senderControl == Control);

console.log('controlEnable',controlEnable);
console.log('DependentControlID', DependentControlID);

var control = document.getElementById(DependentControlID);
console.log('control', control);

control.disabled = !controlEnable;
  } catch (e) {
alert("DependentControlByDropDown : " + e.message);
  }
}

答案 1 :(得分:1)

正如我们在对问题的评论中推断的那样,您正在使用ASP.NET。

要特别注意这一点,因为ASP.NET会覆盖分配的ID以确保它们是唯一的。例如,如果您要...

<asp:Textbox runat="server" id="txtbox1"></asp:Textbox>

...呈现的HTML可能类似于(示例)...

<input id="ctl00$MainContent$txtbox1">

因此,getElementById("txtbox1") 什么都没找到


为防止这种情况发生,您可以将ClientIdMode="static"添加到文本框中:

<asp:Textbox runat="server" ClientIdMode="static" id="txtbox1"></asp:Textbox>

现在呈现的HTML将是以下内容...

<input id="txtbox1">

注意::如果页面上有多个文本框,例如,如果您在GridViewRepeater中使用它,请执行<强烈>不使用ClientIdMode="static"!它将创建重复的ID,这些ID 无效无效。