【问题标题】:clear the selected options after alert message is displayed jQuery显示警报消息后清除所选选项jQuery
【发布时间】:2021-08-30 09:05:45
【问题描述】:
  • 第一个选项是默认选项,因此会检索并比较其他选项中的选项文本。
  • 现在,在显示警告消息后,我想清除在 2 号和 3 号中选择的选项,而不是默认的 1 号选项。

链接https://jsfiddle.net/fswygznq/1/

 function check(){
        var $fruits = [];
        $("select.fruits").each(function() {
          var $two = $(this).find("option:selected").text();
          $fruits.push($two)
        })
        for ($i= 1; $i < $fruits.length; $i++) {
            for ($j= 0; $j < 5; $j++) {
            if ($fruits[$i] == "Apple" && $fruits[$i+$j] == "Banana") {
               alert ("Since you have selected Apple, you cannot select Banana");
              $("select.fruits").val(""); // this one clears all the options including the default**
              return false;
            }
            if ($fruits[$i] == "Banana" && $fruits[$i+$j] == "Apple") {
              alert ("Since you have selected Banana, you cannot select Apple");
               $("select.fruits").val(""); // this one clears all the options including the default
              return false;
            }
            }
        }
    }

【问题讨论】:

    标签: javascript php jquery node.js reactjs


    【解决方案1】:

    使用此选择器时,您只需不要选择第一个选项元素:

    $("select.fruits").val("");
    

    删除它最易读的方法是使用not

    $("select.fruits").not(':first').val("");
    

    但是使用slice 应该更快,因为它不适用于 DOM,它只会从数组中删除元素。

    $("select.fruits").slice(1).val("");
    

    检查下面的sn-p:

    function check() {
      var $fruits = [];
      $("select.fruits").each(function() {
        var $two = $(this).find("option:selected").text();
        $fruits.push($two)
      })
      for ($i = 0; $i < $fruits.length; $i++) {
        for ($j = 0; $j < 5; $j++) {
          if ($fruits[$i] == "Apple" && $fruits[$i + $j] == "Banana") {
            alert("Since you have selected Apple, you cannot select Banana");
            clearFields();
            return false;
          }
          if ($fruits[$i] == "Banana" && $fruits[$i + $j] == "Apple") {
            alert("Since you have selected Banana, you cannot select Apple");
            clearFields();
            return false;
          }
        }
      }
    }
    
    function clearFields() {
      $("select.fruits").slice(1).val("");
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <table>
      <thead>
        <tr>
          <th>Number</th>
          <th>Fruits</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="num">1</td>
          <td>
            <select class="fruits">
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">2</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">3</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">4</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
      </tbody>
    </table>
    <button onclick=check()>
      Submit
    </button>

    更新:仅清除 错误 字段。

    要仅清除产生错误的字段,您可以将错误条件传递给clearFields() 函数并将其用于filter

    clearFields(condition) {
      $("select.fruits").slice(1)
        .filter(function() {
          return $(this).children('option:selected').text() === condition;
        }).val("");
    }
    

    function check() {
      var $fruits = [];
      $("select.fruits").each(function() {
        var $two = $(this).find("option:selected").text();
        $fruits.push($two)
      })
      for ($i = 0; $i < $fruits.length; $i++) {
        for ($j = 0; $j < 5; $j++) {
          if ($fruits[$i] == "Apple" && $fruits[$i + $j] == "Banana") {
            alert("Since you have selected Apple, you cannot select Banana");
            clearFields("Banana");
            return false;
          }
          if ($fruits[$i] == "Banana" && $fruits[$i + $j] == "Apple") {
            alert("Since you have selected Banana, you cannot select Apple");
            clearFields("Apple");
            return false;
          }
        }
      }
    }
    
    function clearFields(condition) {
      $("select.fruits").slice(1)
        .filter(function() {
          return $(this).children('option:selected').text() === condition;
        }).val("");
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <table>
      <thead>
        <tr>
          <th>Number</th>
          <th>Fruits</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="num">1</td>
          <td>
            <select class="fruits">
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">2</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">3</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">4</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
      </tbody>
    </table>
    <button onclick=check()>
      Submit
    </button>

    【讨论】:

    • '对不起,我有一个问题。如果我只想清除发生错误的字段怎么办。比如第2个选择苹果,第3个选择香蕉,第4个选择橙色,现在选择苹果和香蕉是错误的,所以唯一选择了苹果和香蕉的字段应该被清除而不是橙色。
    • 您可以过滤结果元素集并仅清除符合您条件的元素集,请参阅更新@Stephanie。
    • 知道了。谢谢!
    • @Stephanie 为什么您不接受答案?我以为它解决了你的问题。我很好奇为什么其他答案更好,代码 sn-p 对我不起作用,单击按钮时没有任何反应。
    • @Stephanie 谢谢。如果您认为多个答案有用,您可以对它们进行投票,与您认为有用的其他问题一样,请查看 help someone answersvoting 以了解这有什么帮助。
    【解决方案2】:

    为此,您可以像这样利用每个函数回调index argument

    $("select.fruits").each(function(index, element){
               
        index != 0 ? $(element).val("") : null;
                 
    });
    

    function check(){
        var $fruits = [];
        $("select.fruits").each(function() {
          var $two = $(this).find("option:selected").text();
          $fruits.push($two)
        })
        for ($i= 1; $i < $fruits.length; $i++) {
            for ($j= 0; $j < 5; $j++) {
            if ($fruits[$i] == "Apple" && $fruits[$i+$j] == "Banana") {
               alert ("Since you have selected Apple, you cannot select Banana");
              $("select.fruits").each(function(index, element){
               
                    index != 0 ? $(element).val("") : null;
                 
               });
              return false;
            }
            if ($fruits[$i] == "Banana" && $fruits[$i+$j] == "Apple") {
              alert ("Since you have selected Banana, you cannot select Apple");
               $("select.fruits").each(function(index, element){
               
                    index != 0 ? $(element).val("") : null;
                 
               });
              return false;
            }
            }
        }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <table>
      <thead>
        <tr>
          <th>Number</th>
          <th>Fruits</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="num">1</td>
          <td>
            <select class="fruits">
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">2</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="num">3</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
         <tr>
          <td class="num">4</td>
          <td>
            <select class="fruits">
              <option value=""></option>
              <option value="1">Apple</option>
              <option value="2">Banana</option>
              <option value="3">Orange</option>
            </select>
          </td>
        </tr>
      </tbody>
    </table>
    <button onclick= check()>
      Submit
    </button>

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      相关资源
      最近更新 更多