You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于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 the change event won't trigger as expected.
  • Missing "Approved" restore logic: Your current code only handles the "Unapproved" case — you need to bring back the required attribute 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:

  1. jQuery Load Order: The jQuery library script is now loaded first, so the $ function is available when your custom code runs.
  2. $(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.
  3. 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 the change event triggers correctly.
  4. Bidirectional Logic: We now handle both "Approved" and "Unapproved" states. When switching back to "Approved", the required attribute and asterisk are restored.
  5. Cleaner Visibility Toggle: The toggle() method simplifies showing/hiding the asterisk by using a boolean value to control visibility.

内容的提问来源于stack exchange,提问作者EllaB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:12:47