用户必须选择至少一个复选框,一旦选中一个复选框,我希望从所有复选框中删除required
,然后如果用户取消选中,则不选中任何复选框,将required
重新放到复选框上。
我的代码不起作用。我在这里发现了很多类似的答案,但没有一个对我有用,所以我再次问这个问题。
这是jsFiddle
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);
}
<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>
发布于 2017-04-28 20:42:36
您忘记的只是添加on('click', ...
事件处理程序jl/k94stpby/12/。
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);
}
});
<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>
发布于 2020-09-04 21:47:51
出于好奇,我知道这是一个旧线程,但是如果您有两个不同的复选框,并且只有在为每组选中一个复选框时,才需要删除所需的复选框,那么您将如何做呢?
<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组复选框,一组类型和一组颜色。所提供的代码将使所有的代码都是必需的,或者所有的代码都不需要。如何调整这段代码,使其只影响每个集合,因为它是自己的?
例如,如果他们选择类型,而不是颜色,它将删除对类型的要求,但保留对颜色的要求,反之亦然?
发布于 2021-01-18 01:44:36
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);
}
});
https://stackoverflow.com/questions/43688543
复制相似问题