0

我有一个 javascript switch case 语句来重定向页面或显示/隐藏文本框,这取决于用户从下拉框值中选择的内容。它在 Firefox 中运行良好,但在 IE 中运行良好。错误信息如下: SCRIPT5007: Unable to get value of the property 'value': object is null or undefined _index.htm, line 67 character 7

switch (document.form.description.options[description.selectedIndex].value) - IE 很难使用 form.desciprition。

<script language="javascript">
 function fundsource()
   {
       var val;


       for (i=0; i< document.form.description.length; i++)
       {

            switch (document.form.description.options[description.selectedIndex].value)

            {
                case "Select":
                    document.form.scholarship.style.visibility='hidden';
                    document.form.description_other.style.visibility='hidden';
                    break;
                case "Wall of Friends":
                    location.href= some url
                    break;
                case "Scholarship":
                    document.form.scholarship.style.visibility='visible';


                    break;
                case "Other":
                    document.form.description_other.style.visibility='visible';
                    document.getElementById("descriptionspan").style.visibility="visible";
                    break;


            }
       }
   }
</script>

html代码如下:

<tr>
                    <td align="right">Description of the Fund for Your Donation <br /></td>
                    <td>

        <select name="description" id="description" onchange="fundsource()">
                        <option>Select</option>
                        <option>Scholarship</option>
                        <option>Rainbow of Life</option>
                        <option>Wall of Friends</option>
                        <option>General</option>
                        <option>Other</option>

                    </select>
                    <select name="scholarship" id="scholarship" style="visibility:hidden;">
                        <option>Alumni Scholarship</option>
                        <option>Other</option>
                    </select>
                    </td>
                  </tr>
                   <tr>
                    <td align="right" valign="top"><div id="descriptionspan" alt="description" style="visibility:hidden;">Other, please specify:</div></td>
                    <td><input type="text" name="description_other" size="50" alt="Fund Other" style="visibility:hidden;" /></td>
                  </tr>

我将 document.form.description.options[description.selectedIndex].value 更改为 document.form.description.options[description.selectedIndex].text,它仍然不起作用。请指教。TIA。

4

3 回答 3

1

如果您打算使用

document.getElementById("description").value

在您的开关情况下,您必须在选项标签中添加“值”属性,例如

<option value="Scholarship">Scholarship</option>

这段代码也不必在 for 循环中,你可以这样做

function fundsource() {
   var val = document.getElementById("description").value;
        switch (val) {
            case "Select":
                document.form.scholarship.style.visibility='hidden';
                document.form.description_other.style.visibility='hidden';
                break;
            case "Wall of Friends":
                location.href= some url
                break;
            case "Scholarship":
                document.form.scholarship.style.visibility='visible';
                break;
            case "Other":
                document.form.description_other.style.visibility='visible';
                document.getElementById("descriptionspan").style.visibility="visible";
                break;
        }
 }

希望这可以帮助。

于 2013-05-15T13:53:41.283 回答
0

你应该做这样的事情

    function fundsource()
       {
           var val;
           var form=document.getElementByID("formID");
           var description=getElementByID("description");

//why for loop?
           //for (i=0; i< description.options.length; i++)
           //{

                switch (description.value)

                {
                    case "Select":
                        //get option and change it's attr
                        break;
                    case "Wall of Friends":
                        //get option and change it's attr
                        break;
                    case "Scholarship":
                        //get option and change it's attr

                        break;
                    case "Other":
                        //get option and change it's attr
                        break;


                }
           //}
       }
于 2013-05-15T13:50:52.823 回答
0

据我所知,即是怪胎,你应该输入更多的词让它知道

1.更改您的js代码:

switch (document.form.description.options[description.selectedIndex].value)

switch (document.form.description.options[document.form.description.selectedIndex].value)

2.更改您的html代码

<option>Select</option>
<option>Scholarship</option>
<option>Rainbow of Life</option>
<option>Wall of Friends</option>
<option>General</option>
<option>Other</option>

<option value="Select">Select</option>
<option value="Scholarship">Scholarship</option>
<option value="Rainbow of Life">Rainbow of Life</option>
<option value="Wall of Friends">Wall of Friends</option>
<option value="General">General</option>
<option value="Other">Other</option>

如果您不想添加属性value,则应将 js 更改.value.innerText

于 2013-05-15T14:02:32.527 回答