根据下拉值激活复选框

我正在寻找一种 Javascript 解决方案,该解决方案将根据在同一表单的其他地方选择的特定下拉值(在标准 HTML 中,由 SQL 生成)启用(在其中放置检查)表单上的复选框。我还没有写任何代码,所以现在没有任何代码可以在这里展示。20 年 4 月 8 日编辑:根据 Giuliano 的出色帮助,我决定添加我放在一起的代码(不起作用),如下所示:


<%

sSQL = "SELECT OrderTypeID, OrderTypeString FROM LookupOrderTypesII"

Set rsOrderTypes = Server.CreateObject("ADODB.RecordSet")

rsOrderTypes.Open sSQL, oConnGlobal, adOpenStatic, adLockReadOnly 

iTotalRecords = rsOrderTypes.Recordcount


If iTotalRecords > 0 Then

%>


<br>

<b>SERVICE/ORDER TYPE:</b>

<br>


<script type="text/javascript">

    let select = document.getElementById("cboOrderType");

    let checkbox = document.getElementById("chkSkipVenueEventDetails");


        select.addEventListener("change", function () {

        checkbox.checked = select.value == "12|LABOR SALES/PAYMENT";

        });

</script>


<select class="rounded" name="cboOrderType" id="cboOrderType">

    <option value="Select One" selected>Select One</option>

    <option value="-9999" <%=IfSelected(iOrderTypeID, "Not Listed-ADD Custom Type")%>>Not Listed-ADD Custom Type</option>

    <%

        Do While Not rsOrderTypes.EOF 


            iOrderTypeID = Trim(rsOrderTypes("OrderTypeID"))

            sOrderTypeString = Trim(rsOrderTypes("OrderTypeString"))

    %>

        <option value="<%=iOrderTypeID%>|<%=sOrderTypeString%>"<%=IfSelected(sOrderTypeString, sOrderTypeString_NewRequest)%>><%=sOrderTypeString%></option>

    <%

        rsOrderTypes.MoveNext

        Loop

    %>

</select>

<% 

    rsOrderTypes.Close

    Set rsOrderTypes = Nothing


End If

%>

'''


The SQL code generates the following code (as viewed in browser source):


'''

</select>

这是复选框 HTML:


<input type="checkbox" name="chkSkipVenueEventDetails" id="chkSkipVenueEventDetails">&nbsp;<strong>Skip Venue & Event Details</strong>&nbsp;&nbsp;



米琪卡哇伊
浏览 107回答 1
1回答

牧羊人nacy

您可以简单地将事件侦听器添加到选择的“更改”事件中,并根据选择的值更改复选框的“已选中”属性html:<html>&nbsp;<head></head>&nbsp;<body>&nbsp; <select id="select">&nbsp; &nbsp;<option value="a" selected>something</option>&nbsp; &nbsp;<option value="b">check</option>&nbsp; </select>&nbsp; <label>&nbsp; &nbsp;<input id="checkbox" type="checkbox">&nbsp; &nbsp;Checkbox&nbsp; </label>&nbsp; <script>&nbsp; &nbsp;let select = document.getElementById("select");&nbsp; &nbsp;let checkbox = document.getElementById("checkbox");&nbsp; &nbsp;select.addEventListener("change", function () {&nbsp; &nbsp; &nbsp;checkbox.checked = select.value !== "a";&nbsp; &nbsp;});&nbsp; </script>&nbsp;</body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript