开发者

Small Programming confusion regarding if else and else if in jquery

Say i have a table row with the two text boxes and i want to manipulate them based on the screens and some array values

                     开发者_StackOverflow社区       <tr id ="myrow" style="display:none">
    <td>
      test1<input type="text" name="some" value ="some">
      test2<input type="text" name="test2" value ="test2">
    </td>
    </tr>

i am using the same form for all the screens .same form is being rendered for all screens so iam having a hidden value iam manipulating the things based on hidden

  var somearr = ['AAA','BBB','CCC','DDD'];
  //ready function 
  if ($("#hi_showelements").attr('value')=="screen1"){
      $("#firsttd").removeAttr('style'); //we have the td but not shown above

  }
  else if($("#hi_showelements").attr('value')=="screen2"){
     $("#myrow").hide();

  else if($("#hi_showelements").attr('value')=="screen3"){
      $("#myrow").hide();

  }
  else if ($("#hi_showelements").attr('value')=="screen4"){
   $("#myrow").hide();

  }

  if($.inArray(somecode,somearr)=='-1'){
     $("#myrow").hide();

   }
  else{
   $("#myrow").show();
  }

the above row "myrow" is hidden correctly in the scrren1,screen2,screen3,screen4, . But my problem is i cannnot hide the row in the same screens

say if "somecode" value is "AAA" which is present in the "somearr " array and which is not equal to "-1" and the "#myrow" is being shown . How can i hide this row if the "somecode" is present in the "somearr"


What you're trying to do should already work. If it isn't, you should first clean up your code so that it'll be easier to spot errors.

First, I'd suggest replacing the first bunch of if statements with a switch:

switch ($("#hi_showelements").attr('value')) {
  case 'screen1':
    $("#firsttd").removeAttr('style');
    break;

  case 'screen2':
  case 'screen3':
  case 'screen4':
    $("#myrow").hide();
    break;
}

Next, inArray returns an integer, not a string. Just because JavaScript allows you to do compare strings with integers doesn't mean it's a good idea. Use something like this:

if($.inArray(somecode,somearr) == -1)
   $("#myrow").hide();
else
   $("#myrow").show();

If it still doesn't work, maybe somecode isn't set properly. We would need to see where you define it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜