在 jquery 中解锁输入时删除错误文本

发布于 2025-01-11 07:54:22 字数 1601 浏览 0 评论 0原文

我有一个带有选择输入的表单,以及一个在选择选项时锁定我的输入的函数。

我添加了一条警告,表明当有人选择某个选项时输入会被锁定。我想添加一个函数,当有人选择带有 value="" 的选项时删除此警告。

它正在删除我的警告,但例如,当我选择选项文本 1 然后选择文本 2 时,我的警告会显示两次,然后当我选择第一个选项的选项时,它会删除警告,但仅删除第一个。

如何更改它,使警告仅显示一次,而不是多次,并在首先选择选项后将其删除。

$(function() {
  $('#stato').change(function() {
    var value = $(this).val(); //Pobranie wartości z tego selecta
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
      $("#error").remove();
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#data_consegna').after('<div id="error" style="color:red;">Input locked</div>');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>

I have a form with a select, input and a function that locks my input when an option is selected.

I added a warning that inputs are locked when someone selects an option. I would like to add a function to remove this warning when someone chooses an option with value="".

It's removing my warning but for example when I choose option text 1 then text 2 my warning displays twice and then when I choose a selection with first option it removes warning but only first.

How to change it so that the warning displays only once, and not more times, and removes it after select with option first.

$(function() {
  $('#stato').change(function() {
    var value = $(this).val(); //Pobranie wartości z tego selecta
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
      $("#error").remove();
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#data_consegna').after('<div id="error" style="color:red;">Input locked</div>');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音栖息无 2025-01-18 07:54:22

我的警告显示两次

您没有检查它是否已经存在或将其删除。

仅先删除

ID 必须是唯一的,因此 $("#error").remove(); 将仅删除第一个 ID。使用类而不是 ID 来删除多个元素。如果只添加一次,这不会是问题;只是解释为什么它只删除第一个。

正如另一个答案中所述,最好的解决方案是简单地 .show()/.hide(),所以我不会在这里重复。

要更新代码,您始终可以删除错误,然后根据需要将其添加回来 - 如上所述,这不是最有效的。

更新的片段:

$(function() {
  $('#stato').change(function() {
    var value = $(this).val(); //Pobranie wartości z tego selecta
    
    // always remove any existing error
    $("#error").remove();
    
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#data_consegna').after('<div id="error" style="color:red;">Input locked</div>');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>

另请注意,由于它使用 ID,因此只能用于单个错误消息。

my warning displays twice

You don't check if it's already there or remove it.

removes only first

IDs must be unique, so $("#error").remove(); will only remove the first one. Use classes instead of IDs to remove multiple elements. If you only add once, this would not be an issue; just explaining why it removes only the first.

As noted in the other answer, the best solution is to simply .show()/.hide(), so I won't repeat that here.

To update your code, you can always remove the error, then add it back if needed - this isn't the most efficient as noted above.

Updated snippet:

$(function() {
  $('#stato').change(function() {
    var value = $(this).val(); //Pobranie wartości z tego selecta
    
    // always remove any existing error
    $("#error").remove();
    
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#data_consegna').after('<div id="error" style="color:red;">Input locked</div>');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>

Also note, as it's using an ID, it can only be used for a single error message.

叹梦 2025-01-18 07:54:22

实现您所需的简单方法是让通知 div 始终包含在 DOM 中,但隐藏,然后根据 select 的状态隐藏/显示它, 像这样:

jQuery(function($) {
  $('#stato').change(function() {
    var value = $(this).val();
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
      $("#error").hide();
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#error').show();
    }
  });
});
#error {
  color: red;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
  <div id="error">Input locked</div>
</div>

The simple way to achieve what you require is to have the notification div always contained in the DOM, but hidden, and then hide/show it depending on the state of the select, like this:

jQuery(function($) {
  $('#stato').change(function() {
    var value = $(this).val();
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
      $("#error").hide();
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#error').show();
    }
  });
});
#error {
  color: red;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
  <div id="error">Input locked</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文