weixin_33724059 2013-12-05 12:29 采纳率: 0%
浏览 15

jQuery动态加载表单

I am trying to automatically do some form preprocessing whenever a form gets loaded on my website. This can happen via a normal page load, but also via an AJAX inline page load. So putting it in $(document).ready(function() { /* ... */ }); does not suit my needs.

What I have so far:

$("form").each(function() {
    $(this).ready(function() {
        $(this).find(".input input").each(function() {
            var required = $(this).data("required");
            var checkField = $(this).closest("tr").children(".check");
            var errorField = $(this).closest("tr").children(".errormessage");
            if (required) {
                $(checkField).css("color", "#FFFF00");
                $(checkField).html("✘");
                $(errorField).css("color", "#FFFF00");
                $(errorField).html("(Required)");
            }
            else {
                $(checkField).css("color", "#FFFF00");
                $(checkField).html("✔");
                $(errorField).css("color", "#000000");
                $(errorField).html("");
            }
        });
    });
});

When putting it in $(document).ready(function() { /* ... */ }); it works, but only for the first load. When I'm putting it outside that function, then it never works, it does not even reach the $("form").each(function() { /* ... */ });.

How to fix this?

  • 写回答

1条回答 默认 最新

  • weixin_33704591 2013-12-05 12:33
    关注

    Could you make a plugin out of this? For example:

     $.fn.formRendering = function(){
    
         //process your code here
     };
    
     $('form').formRendering();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作