如果选择了选项,则禁用列表

Disable list if option selected

我的代码部分有效。该列表被禁用,但没有发现我选择了 "product1" 然后启用了该列表。选择的任何其他内容都应完全禁用列表。列出带有 ID 的产品。所以我想这与我选择的选项的语法有关,不确定这是否是正确的编写方式。

VBScript

'Disables or enables list based on selection
Function enabler()
    For Each opt In document.GetElementByID("customer").Options
        If opt.Selected = "product1" Then
          document.GetElementByID("product").Disabled = False
        Else
          document.GetElementByID("product").Disabled = True
        End If
    Next
End Function

HTA

...
<select size="5" id="product" name="ListboxUserRole" onChange="SaveListboxUserRoleValue">
    <option value="1" selected="selected">product1</option>
    <option value="2">product2</option>
    <option value="3">product3</option>
...
<select size="5" id="customer" name="ListboxCustomer" onChange="SaveListboxCustomerValue" value="1">
    <option value="1" selected="selected">customer1</option>
    <option value="2">customer2</option>
    <option value="3">customer3</option>
    <option value="4">customer4</option>
...

如果我做对了,当 product1product select 中 select 时,您需要启用 customer select ,如果有其他则禁用。

首先product select变化事件链接到SaveListboxCustomerValue()customer select 禁用应在 SaveListboxCustomerValue() 内处理,将 <body onload="enabler()"> 替换为 <body>

IMO 最好修改算法 以使用 product select 对象的 selectedIndex 属性,enabler() 那么就没有必要了。删除 enabler(),并对 SaveListboxCustomerValue() 进行更改:

Sub SaveListboxCustomerValue()

    ' enable customer if the first item in product selected
    customer.disabled = product.selectedIndex <> 0

    ' the rest part of the code

End Sub

否则,如果您想保留 enabler(),请阅读Option Object Properties。您当前始终具有的条件 returns False,因为布尔值永远不会等于字符串 "product1"。代码应该是这样的:

Sub enabler()

    Dim opt
    Dim match

    For Each opt In product.options
        match = opt.selected And opt.label = "product1"
        If match Then Exit For
    Next
    customer.disabled = Not match

End Sub

enabler() 应在 customer 更改时调用,添加对它的调用:

Sub SaveListboxCustomerValue()

    enabler()

    ' the rest part of the code

End Sub