[英]javascript if statement to do-while
I am trying to get this snip of code to work in a while loop instead of an if. 我正在尝试使这段代码在while循环而不是if中工作。 I need each instant of num_# to not be displayed if the printLoad_# val is empty. 如果printLoad_#val为空,我需要不显示num_#的每个瞬间。 So if printLoad_1 value = nothing, the Num_1 would not be displayed, and then the printLoad_2 would check to see if its num_2 is empty and so on. 因此,如果printLoad_1的值= none,则不会显示Num_1,然后printLoad_2将检查其num_2是否为空,依此类推。 The problem I am having is the function stops before checking each section. 我遇到的问题是该功能在检查每个部分之前停止。 Im not sure if a do-while will work. 我不确定是否需要一段时间。
$(document).ready(function() {
if(document.getElementById("printLoad_1").value == "")
{
document.getElementById('num_1').style.display = 'none';
}
if(document.getElementById("printLoad_2").value == "")
{
document.getElementById('num_2').style.display = 'none';
}
if(document.getElementById("printLoad_3").value == "")
{
document.getElementById('num_3').style.display = 'none';
}
if(document.getElementById("printLoad_4").value == "")
{
document.getElementById('num_4').style.display = 'none';
}
if(document.getElementById("printLoad_5").value == "")
{
document.getElementById('num_5').style.display = 'none';
}
});
Have you considered just compositing the strings instead of this verbose construction? 您是否考虑过只组合字符串而不是这种冗长的结构? Something like this: 像这样:
for( var i=1; i<=5; i++ ) {
if( document.getElementById('printLoad_'+i).value === '' ) {
document.getElementById('num_'+i).style.display = 'none';
}
}
Assuming you're using jQuery and that your elements are in order, I'd forget about ID's. 假设您使用的是jQuery,并且您的元素顺序正确,那么我会忘记ID。 If you use common classes, say .printload
and .num
, then you can easily target elements by index like: 如果使用通用类,例如.printload
和.num
,则可以轻松地通过索引来定位元素,例如:
$('.printload').each(function(i){
if (!this.value) $('.num').eq(i).hide();
});
if you have a variable amount of printLoad and num: 如果您有可变数量的printLoad和num:
var i = 1,
pl=document.getElementById('printLoad_'+i),
num = document.getElementById('num_'+i);
while(pl !== null && num !== null){
if(pl.value === ""){
num.style.display = 'none';
}
i++;
pl=document.getElementById('printLoad_'+i),
num = document.getElementById('num_'+i);
}
Here is a slight modification of Ethan's answer that "works dynamically". 这是对Ethan回答“动态工作”的略微修改。 I've also updated it to use jQuery. 我还更新了它以使用jQuery。 This could be handled cleaner if CSS classes and a hierarchy relationship were used, but that would affect how the DOM needed to be generated .. 如果使用CSS类和层次结构关系,则可以更简洁地进行处理,但这将影响需要如何生成DOM。
for (var i = 1; /* break */; i++) {
var printEl = $('#printLoad_' + i)
if (printEl.length) {
if (!printEl.val()) {
$('#num_' + i).css({display: 'none'})
}
} else {
// No #printLoad_N, guess we're done looking
break
}
}
Per @elclanr 's answer: 根据@elclanr的回答:
Use common classes, and target elements by index, it'll be much simpler. 使用通用的类,并按索引指定目标元素,它将更加简单。
Set your "printLoad" elements to class='printLoad'
and your "num" elements to class='num'
. 将您的“ printLoad”元素设置为class='printLoad'
,将您的“ num”元素设置为class='num'
。 Then... 然后...
for (i=0;i<document.getElementByClass('printLoad').length;i++)
{
if (document.getElementByClass('printLoad')[i].value == "")
{
document.getElementByClass('num')[i].style.display='none';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.