禁用列表,如果选项选中(Disable list if option selected)

编程入门 行业动态 更新时间:2024-10-06 16:17:25
禁用列表,如果选项选中(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>
...

My code is partially working. The list gets disabled but it's not catching that I have selected "product1" and then enables the list. Anything else chosen should disable the list completely. List with ID product. So I guess it's something with my syntax for the option selected, not sure if thats the correct way to write it.

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>
...

                

最满意答案

如果我得到了正确的结果,您需要在product select中选择product1时启用customer选择,如果有其他选项,则禁用。

首先 , product选择更改事件链接到SaveListboxCustomerValue() 。 应在SaveListboxCustomerValue()内处理customer选择禁用,用<body onload="enabler()">替换<body onload="enabler()"> <body> 。

IMO更好地修改算法以使用product选择对象的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() ,请阅读有关选项对象属性 。 您现在总是返回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

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

Sub SaveListboxCustomerValue() enabler() ' the rest part of the code End Sub

If I get it right, you need to enable the customer select when product1 is selected in product select, and disable if any other.

First of all, product select change event is linked to SaveListboxCustomerValue(). customer select disabling should be processed within SaveListboxCustomerValue(), replace <body onload="enabler()"> with just <body>.

IMO better to rework the algorithm to use the selectedIndex property of the product select object, the enabler() is not necessary then. Remove enabler(), and make changes to 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

Otherwise, if you want to keep the enabler(), then read about Option Object Properties. The condition you currently have always returns False, since boolean value never equals the string "product1". The code should be like this:

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() should be called on customer change, add call to it:

Sub SaveListboxCustomerValue() enabler() ' the rest part of the code End Sub

更多推荐

本文发布于:2023-08-07 18:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   列表   Disable   selected   option

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!