Jump to content

converting variables


php_novice2007

Recommended Posts

hi,

 

I've got the following

 

<input type=checkbox name="u0001" unchecked>u0001<br>

<input type=checkbox name="u0002" unchecked>u0002<br>

<input type=checkbox name="u0003" unchecked>u0003<br>

<input type=checkbox name="u0004" unchecked>u0004<br>

 

and I want to use javascript to go through all the check boxes and see which one is checked. The check box names are stored in an array called unitArray. unitArray[0] = "u0001", unitArray[1] = "u0002" etc.

 

In my code I've got

 

for (var i = 0; i < unitArray.length; i++) {
  var t = "myform." + unitArray[i] + ".checked"; 
  if (t) {

  }
}

 

But this does not work. It says that myform.unitArray has no properties. Seems like the if statement just sees a string and say it is true because it is non empty.

 

How do I do what I want..?

 

Thanks~!

Link to comment
Share on other sites

Hi,

Thanks for that, that part works now.

Now I have a series of radio buttons

[code]
<input type=radio name="u0001choice" checked value="u0001 yes">yes
<input type=radio name="u0001choice" value="u0001 no">no
<input type=radio name="u0002choice" checked value="u0002 yes">yes
<input type=radio name="u0002choice" value="u0002 no">no
<input type=radio name="u0003choice" checked value="u0003 yes">yes
<input type=radio name="u0003choice" value="u0003 no">no
<input type=radio name="u0004choice" checked value="u0004 yes">yes
<input type=radio name="u0004choice" value="u0004 no">no 

               

 

And I want to go through the list of unitArray again and see which button is selected..

 

I tried

 

for (var i = 0; i < unitArray.length; i++) {
  var t2 = unitArray[i] + "choice"; 
  var t3 = document.myform[t2][1].checked;
  var t4 = document.myform[t2][1].value;              
  if (t3) {
    alert("For" + unitArray[i] + " " + t4 + " is chosen");
  }
}

 

But nothing is being alerted :(               

                }

[/code]

Link to comment
Share on other sites

-do not use 'var' inside a loop, use 'var' before the loop

-your extra '[1]' was not valid

var t2, t3, t4;
for (var i = 0; i < unitArray.length; i++) {
    t2 = unitArray[i] + "choice"; 
    t4 = document.myform[t2].value;
    // it should alert a value like 'u0001 yes' or 'u0001 no'  but not both            
   alert("For " + t2 + " " + t4 + " is chosen");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.