首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果选中复选框,请删除所需

如果选中复选框,请删除所需
EN

Stack Overflow用户
提问于 2017-04-28 20:38:35
回答 4查看 2.6K关注 0票数 2

用户必须选择至少一个复选框,一旦选中一个复选框,我希望从所有复选框中删除required,然后如果用户取消选中,则不选中任何复选框,将required重新放到复选框上。

我的代码不起作用。我在这里发现了很多类似的答案,但没有一个对我有用,所以我再次问这个问题。

这是jsFiddle

代码语言:javascript
运行
复制
var checkbox_required = $('input[type="checkbox"]');

checkbox_required.prop('required', true);

if (checkbox_required.is(':checked'))
{
	checkbox_required.prop('required', false);
}
else
{
	checkbox_required.prop('required', true);
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="奢侈品与配饰">奢侈品与配饰
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="女士时尚">女士时尚
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="男士时尚">男士时尚
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="美妆与护肤">美妆与护肤
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="儿童">儿童
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="内衣">内衣
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="家居">家居
  </label>
  <input type="submit" value="submit">
</form>

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-04-28 20:42:36

您忘记的只是添加on('click', ...事件处理程序jl/k94stpby/12/

代码语言:javascript
运行
复制
var checkbox_required = $('input[type="checkbox"]');

checkbox_required.prop('required', true);

checkbox_required.on('click', function(){
    if (checkbox_required.is(':checked')) {
        checkbox_required.prop('required', false);
    } else {
        checkbox_required.prop('required', true);
    }
});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<form>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="奢侈品与配饰">奢侈品与配饰
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="女士时尚">女士时尚
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="男士时尚">男士时尚
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="美妆与护肤">美妆与护肤
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="儿童">儿童
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="内衣">内衣
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="家居">家居
  </label>
  <input type="submit" value="submit">
</form>

票数 2
EN

Stack Overflow用户

发布于 2020-09-04 21:47:51

出于好奇,我知道这是一个旧线程,但是如果您有两个不同的复选框,并且只有在为每组选中一个复选框时,才需要删除所需的复选框,那么您将如何做呢?

代码语言:javascript
运行
复制
<form>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="blah"
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_type[]" value="blah blah"
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_color[]" value="red"
  </label>
  <label class="produit_type">
    <input type="checkbox" name="produit_color[]" value="green"
  </label>
    <input type="submit" value="submit">
</form>

只是保持表格简单。因此,2组复选框,一组类型和一组颜色。所提供的代码将使所有的代码都是必需的,或者所有的代码都不需要。如何调整这段代码,使其只影响每个集合,因为它是自己的?

例如,如果他们选择类型,而不是颜色,它将删除对类型的要求,但保留对颜色的要求,反之亦然?

票数 0
EN

Stack Overflow用户

发布于 2021-01-18 01:44:36

代码语言:javascript
运行
复制
var checkbox_required = $('input[type="checkbox"]');

checkbox_required.attr('required', true);

checkbox_required.on('click', function(){
    if (checkbox_required.is(':checked')) {
        checkbox_required.attr('required', false);
    } else {
        checkbox_required.attr('required', true);
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43688543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档