javascript - 使用 JQuery 创建后未检查具有必需 Prop 的输入

标签 javascript jquery html css cakephp

我正在处理一个包含“替代地址”的联系表 <div id='alt'>具有可切换的可见性。它包含一个 required <input> field 。起初我遇到了问题,在切换 #alt 之后两次(因此不显示它,因为它在开始时设置为 display:none),仍会检查必填字段是否有 value ,即使它不应该。发生这种情况是因为我实现了一个 jQuery 函数来替换“填写此字段”字符串,如果 required 会弹出该字符串。字段未填写。

$('.submit').click(function () {
            $('input').filter('[required]:visible').each(function () {
                if ($(this).attr('type') == 'text') {
                    if ($(this).val() == '') {
                        this.setCustomValidity("custom text");
                    } else {
                        this.setCustomValidity("");
                    }
                }
            });
            $('')
        });

由于处理程序应用于元素的方式,我被迫完全删除并重新创建 #alt 的内容切换时的 div。

$('#open_alt').click(function () {
            $(this).hide();            //hide the "show" button
            $('#close_alt').show();    //show the "hide" button
            $("#alt").fadeToggle();    //show the #alt div
            $(this).next().next().html(blockhtml); //re-create the content of the #alt div
            $('#OrderPhoneAlt').prop('required', 'required'); //this probably isnt necessary, but I wanted to make sure that the required input field in question really has the necessary prop
        });
$('#close_alt').click(function () {
            $(this).hide();             //hide the "hide" button
            $('#open_alt').show();      //show the "show" button
            $("#alt").fadeToggle();     //hide the #alt div
            blockhtml = $(this).next().html(); //store the content of the #alt div
            $(this).next().html(''); //empty the #alt div
        });

现在,在切换 #alt 之后div 两次,浏览器不检查我的 required <input>不再,即使它有 required - Prop 。

这显然与重新创建 html 元素有关,但有没有办法解决这个问题?

这个元素是基于 CakePHP 1.3 的,如果那很重要的话,但我不这么认为。

最佳答案

你可以尝试改变

$('#OrderPhoneAlt').prop('required', 'required'); 

$('#OrderPhoneAlt').prop('required', true);

如果不起作用,请发布您的 html 输出和 blockhtml 内容

关于javascript - 使用 JQuery 创建后未检查具有必需 Prop 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316394/

相关文章:

javascript - Web Audio API 延迟反馈导致 Chrome 崩溃

Javascript : Do Action after elapsed time

javascript - jQuery UI 工具提示在鼠标移开时不会关闭 div

javascript - 在 asp.Net LinkBut​​ton OnClientClick 上触发 html5 表单验证

javascript - 使用 jquery 创建带有 div 的框

javascript - 使用 yield 作为异步控制流有什么好处?

javascript - 将 `jquery` 添加到 tsconfig 中的 types 字段

php - 根据第一个动态下拉列表中的选择填充第二个动态下拉列表的 Ajax 方法

javascript - 如何将单选输入与文本输入相结合

html - CSS:匹配页面宽度的表格布局