开发者

dynamic html forms

I am working on dynamic form. i have a radio button, if a user select a radio button, i want to populate a drop down menu based on the radio button values.

eg. if radio button value=value1 show drop menu of 3 items (apple, banana, mango)

eg. if radio button value=value2 show drop menu of 5 items (apple, banana, mango,apricot, ornage)

etc. I have used css to hide and show the drop down menu.

(1) the problem i am having is, this doesn't work when i insert my form in a <table></table>; when i remove the table tags it works. it开发者_运维知识库s a bit strange...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--

function changeDiv(the_div,the_change)
{
  var the_style = getStyleObject(the_div);
  if (the_style != false)
  {
    the_style.display = the_change;
  }
}

function hideAll()
{
  changeDiv("main_category","none");
  changeDiv("another_category","none");
  changeDiv("other_category","none");
}

function getStyleObject(objectId) {
  if (document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
    return document.all(objectId).style;
  } else {
    return false;
  }
}
// -->
</script>
</head>

<body>
<table>

<tr>
  <td align="right" valign="top">Main Category:</td>
  <td>
  <input type="radio" name="main_cat" onClick="hideAll(); changeDiv('main_category','block');" value="value1">value1<br />
  <input type="radio" name="main_cat" onClick="hideAll(); changeDiv('main_category','block');" value="value2">value2<br />
  <input type="radio" name="main_cat" onClick="hideAll(); changeDiv('main_category','block');" value="value3">value3<br />
  <input type="radio" name="main_cat" onClick="hideAll(); changeDiv('main_category','block');" value="value4">value4
  </td>
</tr>


<div id="main_category" style="margin-left:30px;display:none">
<tr>
  <td align="right">Options:</td>
  <td></td>
</tr>
</div>

<tr>
  <td align="right">Aanother Category:</td>
  <td>
  <input type="radio" name="another_cat" onClick="hideAll(); changeDiv('another_category','block');" value="Yes">Yes
  <input type="radio" name="another_cat" value="No">No
  </td>
</tr>


<div id="another_category" style="margin-left:30px;display:none;">
<tr>
  <td align="right">Other Category:</td>
  <td>

  </td>
</tr>

<tr>
  <td align="right">Interest:</td>
  <td>

  </td>
</tr>
</div>





</table>
</body>
</html>

(2) second question is, i have a second radio buttons like this: eg. if radio button value=value1 show drop menu of 3 items (apple, banana, mango) eg. if radio button value=value2 show drop menu of 5 items (apple, banana, mango,apricot, ornage) etc. I have used css to hide and show the drop down menu.

now the trick is, if i already selected value=value1, i don't want to show value1 again in the second radio button

any help/suggestions is greatly appreciated.


try this

<html>
    <head>
        <script type="text/javascript">
                     var comList= new Array("apple","banana","mango");
                     var list = new Array("orage", "apricot");
                     var val1selected = 0;  

             function loadDropDown(option){
                         setSelectdVal(option)
                         createDropDown(option)
                     }  
                 function setSelectdVal(option){
                         if (option == 1){
                           val1selected = 1; 
                         } 
                     }       

                     function createDropDown(option){
                        html = "";
                        html = html + '<select id="id">'
                        var arr = getArray(option);
                        for (var i=arr.length-1; i>=0; --i ){
                            html = html + '<option value="' + i + '">' + arr[i] + '</option>' 
                        }
                        html = html + '</select>'
                        document.getElementById('selectTag').innerHTML = html;
             }
                     function getArray(option){
                        var a = comList;
                        if ((option == 2) && (val1selected == 0)){
                           a = comList.concat(list)
                        }
                        return a;
                     }

                </script>
    </head>
        <body>
        <table>
                    <tr>
                        <td><input type="radio" name="options" onClick=loadDropDown(1);>value1</td>
                        <td><input type="radio" name="options" onClick=loadDropDown(2);>value2</td>
                    </tr>
            <tr>
                        <td></td>
                        <td id="selectTag"></td>
                    </tr>   
                </table>
        </body> 
</html>


I would suggest taking a look at some Javascript frameworks which will make your life a lot easier. Take a look at ExtJS: http://www.sencha.com/products/js/ or jQuery: http://jquery.com/ these frameworks can handle your problems with ease.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜