2013-01-25 20 views
0

我之前曾问过这个问题,但我的客户想要在调查问卷上创建一些功能。他希望所有的“注释文本字段”都会被隐藏,如果它是空的。它只有在“注释文本字段”有价值时才可见。如果有值或无值,也需要更新/检查是/否单选按钮。这是以前的“失眠症”做的代码。多个调查问卷

$('.notes').hide(); 
$('input[type="radio"]').change(function() { 
    if ($(this).val() == "Yes") { 
    $(this).closest('tr').next('tr.notes').slideDown(); 
    } else { 
    $(this).closest('tr').next('tr.notes').slideUp(); 
    } 
}); 

不过,我需要先验证文本字段,如果有一个值(实际上是1到33的文本字段将在这里使用它的绝大部分将有值)。

var vals = $('.notes input[type="text"]').val(); 
if(vals == "") { 
    console.log('negative'); 
    $('tr.notes').slideUp(); 
} else { 
    console.log('positive'); 
    $('.query input[value="Yes"]').attr({ 
    checked: "checked" 
    }); 
    $('tr.notes').slideDown(); 
} 

这里就是我讨论...... jQuery Bind or Index FormInsomniac Demo

+0

* *当你做文本字段验证和向上滑动?文本字段必须在空的某个点上显示正确吗? –

回答

0

经过一番研究和非停止工作的脚本。我终于决定将代码更新为基本设置。 “我不是一个程序员”,其实......

$(".report-notes fieldset").each(function (i) { 
i = i+1; 
$(this).addClass("item"+i); 
$('.item'+i+' input[type="radio"]').on('change',function(){ 
    if($(this).val() == 'Yes') { 
     $('.item'+i+' .notes').slideDown(); 
    } else { 
     $('.item'+i+' .notes').slideUp(); 
    } 
}); 

$(document).ready(function(){ 
    var notes = $('.item'+i+' .notes input[type="text"]').val(); 
    if(notes === '') { 
     console.log('walang laman'); 
     $('.item'+i+' .notes').hide(); 
    } else { 
     console.log('meron laman'); 
     $('.item'+i).find('input[value="Yes"]').attr({ 
      checked: "checked" 
     }); 
     $('.item'+i+' .notes').slideDown(); 
    } 
}); 
}); 

Updated Fiddle Source

1

您需要检查.html()的长度和从视图中相应地删除它的原始格式。

if ($(element).html().length == 0) 
    $(element).hide(); 

如果这些票据将要静态加载时加载该文档,真棒,你只需要做到这一点:

$('.notes').each(function(){ 
    if ($(this).html().length == 0) 
     $(this).hide(); 
}); 

另外,如果你是动态加载它们,一旦你加载它们,使用第一个代码检查空白。

1

//将焦点设置为输入,并且以这种方式我们可以检查模糊事件中输入的任何内容。

 $('.notes').hide(); 

     $('input[type="radio"]').change(function() { 
      if ($(this).val() == "Yes") { 
       $(this).closest('tr').next('tr.notes').slideDown(); 
       $(this).closest('tr').next('tr.notes').find('input').focus();         
      } else { 
       $(this).closest('tr').next('tr.notes').slideUp(); 
      } 
     }); 

//模糊事件捆绑到所有输入

 $('input').blur(function(){ 
     if ($(this).val().length == 0){  
    $(this).closest('tr.notes').slideUp(); 
     } 
    }); 

//更新小提琴