【问题标题】:Getting to grips with JQuery掌握 JQuery
【发布时间】:2011-05-29 23:32:15
【问题描述】:

我想在文档加载时执行“类似这样的操作”:

$(':input:visible').each(function(i){
                                  
    if($(this.getAttribute('required')!=null)) { $(this).css({ 'border-color':'#800000' }); }                               
                                  
});

简单来说,我有:

$(document).ready(function() {
    $(':input:visible').each(function(i){
    if($(this.getAttribute('required')!=null))
    { $(this).css({ 'border-color':'#800000' }); }  
    });
    });

有什么原因导致它不起作用?

【问题讨论】:

  • AHHH ... 发现了一些东西,所以这是一个编辑 - 请。代码“确实有效”,但前提是在实际页面上,表单所在的页面通过 JQuery 加载到空 div 中。有客场比赛吗?
  • @Russell 你能澄清一下内容是如何添加的吗?这可能只是添加内容后运行代码的情况。
  • 嗨,安德烈斯,像这样 - $('#adminwrapinner').load('inc/frm_create.php'); - 页面打开时自动加载
  • @lonesomeday - 它在 .load() 内容代码之后“运行”
  • 认为这可能是我需要“改变”的东西,因为我正在使用带有空 div 的母版页并根据各种操作加载每个内容,这样我可以使用 setInterval 刷新 div根据需要,无需页面加载

标签: jquery each


【解决方案1】:

这一行

if($(this.getAttribute('required')!=null)) {

很奇特。

你想使用attr函数:

if ($(this).attr('required') != null) {

或者,更好更简单的,一个对象属性:

if (this.required) {

【讨论】:

  • 嗨 lonesomeday 谢谢,但如果你回来看看我已经发表了评论。使用 .load('..') 将表单 (/s) 动态加载到空 div 中。如果在“表单”页面上而不是在母版页上,代码是否有效。有什么办法找到的吗?
【解决方案2】:

使用此代码它可以正常工作

$(document).ready(function() {
    $('input:visible').each(function(i){
        if($(this).attr('required') != undefined)
            $(this).css({ 'border-color':'#800000' });  
    });
});

【讨论】:

  • 对不起,但我可以向你保证,除非它在加载到空 div 的页面上,否则它在“主”页面上不起作用
  • 你可以在 .load() 回调中调用它
【解决方案3】:

试试这个:

风格:

input.requiredcss { 'border-color':'#800000'; }

JS:

$('#adminwrapinner').load('inc/frm_create.php', function(response, status, xhr) {

   var adminwrapinner = this;

   if (status == "success") {

     $(':input:visible:not(.requiredcss)', adminwrapinner).each(function(i){

        if($(this).attr('required')) { 
           $(this).addClass("requiredcss"); 
        } 

     });
   }   
});

【讨论】:

  • 谢谢你,但我想我在你发布它的时候到了那里,真的很抱歉你没有得到“勾号”,但如果你有机会让我能理解更多- 希望其他人 - 你的功能(响应,状态,xhr)和只是功能()的“我的”版本之间有什么区别 - 正如对“滴答声”所说的非常感谢和抱歉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 2021-10-09
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多