我的函数跳过我的if then else语句

时间:2013-12-07 10:28:31

标签: javascript

我有这个功能可以扫描超过9个元素。我现在已经进行了4个多小时的故障排除。问题是我希望警报框只出现一次,如果它从其中一个元素中检测到“无”值。另一个是当其中一个元素不等于“无”时,其他元素忽略此语句:

                  if (obj.value == "none"){
                      alert("You must specify the item's size!");      
                  }  

这是整个功能:

function swapImage(id,primary,secondary) {
     src=document.getElementById(id).src;


    if (src.match(primary)) {


                  var arr = new Array();
                  arr = document.getElementsByName('jumpMenu');

                  alert("total objects with name \"jumpMenu\" = \n" + arr.length);

                  for(var i = 0; i < arr.length; i++)
                  {
                      var obj = document.getElementsByName('jumpMenu').item(i);
                    //   alert(obj.id + " =  " + obj.value);

                       if (obj.value == "none"){    

                          alert("You must specify the item's size!");      
                      }  

                      else
                      {   

                      document.getElementById(id).src=secondary;     

                      }
                }
    }
    else 
    {
      location.href='blog.php';
    }
  }

它很长,因为我有9个,所以生病只是插入其中一个元素:

<table width="200" border="0" align="center">
          <tr>
            <th width="63">Price:</th>
            <th width="127"> <?php echo $final_array[0][1]; ?></th>
          </tr>
          <tr>         
            <th>Size:</th>
            <form method="post" name="form" id="form" action="blog.php">
            <th><select name="jumpMenu" id="jumpMenu" class="hirap">
                <option value ="none">Enter Item Size</option>
                <option value ="S">6</option>
                <option value ="M">8</option>
                <option value ="L">10</option>
                <option value ="XL">12</option>
                <option value ="XXL">14</option>
              </select>
            </th>
            <th><img
                    id="tadi"
                    onclick="swapImage(
                      'tadi',
                      'images/submit.gif',
                      'images/so.gif'
                    )"                      
                    src="images/submit.gif"
                    title="View Order"
                  />            

          </th>
            </form>
          </tr>
      </table>

2 个答案:

答案 0 :(得分:1)

我不能立即了解什么是“有效”数据。以下哪一项为您描述了有效数据?

  1. 所有菜单都应该选择none以外的值,(6108none6会无效,但6108126有效)
  2. 至少有一个菜单应该选择none以外的值(6108none6会有效,但nonenonenonenonenone无效。
  3. 对于案例1,引入一个记录是否已指定所有大小的变量,如果有,则在循环结束时执行必要的操作:

                var allOkay = true;
                for(var i = 0; i < arr.length; i++)
                {
                    var obj = document.getElementsByName('jumpMenu').item(i);
                    //   alert(obj.id + " =  " + obj.value);
    
                    if (obj.value === "none"){    
    
                        alert("You must specify the item's size!");      
                        allOkay = false;
                        break;
                    }  
                }
    
                if (allOkay)
                {
                    document.getElementById(id).src=secondary;
                }
    

    变量allOkay指定我们到目前为止检查的所有菜单是否都选择了none以外的值。我们在循环开始时将其设置为true,因为此时我们未找到任何选择了none的菜单。

    然后,如果我们找到一个选择none的菜单,我们会显示提醒,并通过将none设置为{来记录我们现在找到了选中了allOkay的菜单{1}}。我们还false,退出break循环。没有必要再进一步了;一旦我们找到一个价值设置为for的菜单,我们也可以停止。

    在循环结束时,变量none将记录所有菜单是否都选择了allOkay以外的值。

    在我看来,如果未显示警告框,您只需要运行第none行。在这种情况下,放置它的最佳位置是在循环之后,并且只有在所有菜单都选择了document.getElementById(id).src=secondary;以外的值时才运行它。

    案例2类似。在这种情况下,我们会跟踪到目前为止我们找到的所有菜单是否都设置为none,如果是,请在循环结束时报告消息:

    none

答案 1 :(得分:0)

所有试图帮助我的人......非常感谢我解决了这个问题......我解决了问题:

<script type="text/javascript">
function swapImage(id, primary, secondary,ide) {
src = document.getElementById(id).src;

if (src.match(primary)) {

    var allNone = true;

    var arr = new Array();

    arr = document.getElementById(ide);
  //  alert(arr.value +"asd"+ arr.length);

    for(var i = arr.length - 1; i >= 0; i-=1) {

        // var obj = arr.getElementsByName('jumpMenu').item(i);

        // alert(obj.value);

       if (arr.value != "none"){    

                document.getElementById(id).src=secondary;
                allNone = false;
                break;
            }  
    }
      if (allNone)
        {
            alert("You must specify the item's size!");
        }

} else {
    location.href = 'blog.php';
}

}