jquery - 单击时,隐藏 div 并删除所需的属性

标签 jquery html input attributes

我有一个可以出现在不同页面上的表单,根据情况,这些字段并不总是必需的。

我有隐藏 div 的代码,但需要不再需要输入字段。使用 HTML5 Required 属性。无法使代码正常工作。代码如下:

$('#detailssame').click(function() {
    if($(this).is(':checked')) {
        $(\"#detailssamehide\").hide();
    } else {
        $(\"#detailssamehide\").show();
    }               
});
$('.fullboxform input[type=text], .fullboxform select').each(function() {
            $(this).removeAttr('required');
});

非常感谢所有帮助。

事实证明上面的代码确实工作正常,但是使用 prop 可以得到替代答案

最佳答案

尝试使用 prop()用于设置 HTML5 属性的函数。

$(function () {
    $('#detailssame').click(function() {
        var checked = $(this).is(':checked');
        if(checked) {
            $("#detailssamehide").hide();
        } else {
            $("#detailssamehide").show();
        }     
        $('.fullboxform input[type=text], .fullboxform select').each(function() {
            $(this).prop('required', !checked);
        });
    });
});

http://jsfiddle.net/ThsXU/

关于jquery - 单击时,隐藏 div 并删除所需的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887462/

相关文章:

javascript - 如何使用jquery通过具有特殊字符的名称属性来定位输入元素?

html - 如何使用 CSS3 为字体粗细设置动画?

html - 为什么开发工具提供 Quirks 模式?

regex - HTML 输入模式不起作用

javascript - 为什么我们不能使用 css 为 Canvas 设置宽度和高度

javascript - jQuery 下拉菜单一遍又一遍

jquery .load 在 iframe 上添加覆盖 css 样式

javascript - 单击后更改最大高度元素

php - 如何将 ByteArray(来自 Flash)和一些表单数据发送到 php?

file - Logstash 不读取文件输入