基于JavaScript/JQuery实现选中指定选项时移除输入框required属性并隐藏红色星号
Fixing Your Approval Dropdown Logic
Hey Ella, let's dig into why your code isn't working — there are a few small but crucial issues tripping you up:
- jQuery loading order is wrong: You placed your custom jQuery code before loading the jQuery library itself. Browsers run code top to bottom, so when your
$('#Approval').on('change')runs, the$function (jQuery) isn't defined yet. - Invalid
<select>tag syntax: You used a self-closing<select .../>tag, but in HTML,<select>needs to be a non-self-closing tag that wraps its<option>elements. This breaks the DOM structure, so the dropdown might not function correctly, and thechangeevent won't trigger as expected. - Missing "Approved" restore logic: Your current code only handles the "Unapproved" case — you need to bring back the
requiredattribute and show the red asterisk when the user switches back to "Approved".
Here's the fully fixed, working version of your code:
<!-- Load jQuery FIRST, before your custom script --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> // Wait for the entire DOM to load before attaching events $(document).ready(function() { $('#Approval').on('change', function() { const selectedValue = $(this).val(); const isUnapproved = selectedValue === "NO"; // Toggle the red asterisk: hide if Unapproved, show otherwise $("#hideme").toggle(!isUnapproved); // Toggle the required attribute if (isUnapproved) { $("#hide").removeAttr('required'); } else { $("#hide").attr('required', 'required'); } }); }); </script> <!-- Wrap table rows in a <table> tag (required for valid HTML) --> <table> <tr> <td class="Label" align="left"> Type: </td> <td class="InputValue" align="left"> <!-- Fixed <select> tag (no self-closing, properly wraps options) --> <select class="DropdownBox" name="Approval" id="Approval"> <option value=""></option> <option value="YES">Approved</option> <option value="NO">Unapproved</option> </select> </td> </tr> <tr> <td class="Label" align="left"> Comment:<span id="hideme" style="color: #ff0000;">*</span> </td> <td class="InputValue" align="left"> <input class="InpTxt" name="txtAppName" type="text" id="hide" required/> </td> </tr> </table>
Key Fixes Explained:
- jQuery Load Order: The jQuery library script is now loaded first, so the
$function is available when your custom code runs. $(document).ready(): This wrapper ensures your code only executes after all HTML elements are fully loaded — no more trying to interact with elements that don't exist yet.- Valid
<select>Tag: The<select>is properly opened and closed, so all<option>elements are nested inside it. This fixes the dropdown functionality and ensures thechangeevent triggers correctly. - Bidirectional Logic: We now handle both "Approved" and "Unapproved" states. When switching back to "Approved", the
requiredattribute and asterisk are restored. - Cleaner Visibility Toggle: The
toggle()method simplifies showing/hiding the asterisk by using a boolean value to control visibility.
内容的提问来源于stack exchange,提问作者EllaB
相关产品推荐
相关产品推荐

