Javascript:根据下拉列表的当前值显示/隐藏行

时间:2012-06-01 19:08:00

标签: javascript asp-classic

我在这种情况下使用Javascript和ASP。打开此特定页面时,我的下拉菜单之一已填充状态为“打开”或“已关闭” - 您可以看到该值来自我的记录集中的ID。

我现在要做的是:如果首次加载时页面上的状态为“已关闭”且用户决定将其更改为“打开”,则必须重新输入“重新打开原因” - 所以,这将在下拉列表下方显示标题和文本框....

这是我到目前为止所尝试的内容:我创建了一个showHide()函数并将其放在下拉列表中的select中,但它没有做任何事情,所以我现在卡住了。任何帮助表示赞赏。谢谢!

  <select name="cboStatus" id="cboStatus" style="width:200px" onchange="showHide();">                           <%
                        RSStatus.MoveFirst
                        If Not RSStatus.EOF Then
                            Do While Not RSStatus.EOF
                                %><option value='<%= RSStatus("ID")%>'
                                <%If RSStatus("ID") = RS("prjStatus") Then Response.Write "selected"%>><%= RSStatus("prjStatus")%></option><%
                                RSStatus.MoveNext
                            Loop
                        End If
                        %>           
                    </select>

应该从上面的JS中生成的HTML:

<tr id="lbReopenReason" style="display:none">
                <td bordercolor="#f0f0e4" bgcolor="#f0f0e4"><h3>Reopen Reason</h3></td>
              </tr>
              <tr id="trReopenReason" style="display:none">
                <td bordercolor="#FFFFFF" bgcolor="#FFFFFF">
                <input name="txtReopenReason" type="text" id="txtReopenReason" value="<%If (RS("reOpenReason")) <> "" Then Response.Write(RS("reOpenReason"))%>" size="100" />
                </td>
              </tr>     

使用Javascript:

    function showHide()
        {
            var cboStatus = document.getElementById("cboStatus");
                  var cboStatusValue = cboStatus.options[cboStatus.selectedIndex].text;
            var lbReopenReason = document.getElementById("lbReopenReason");             var trReopenReason = document.getElementById("trReopenReason");         

            //If the status of the project is Closed at the time of page load, and that status changes, then the user must enter a re-open reason.
            if ( (status == 3) && (cboStatusvalue == 'Open' )
            {
                lbReopenReason.style.display = "";
                trReopenReason.style.display = "";
                }
                  else
{
                lbReopenReason.style.display = "none";
            trReopenReason.style.display = "none";
            }
    }  

2 个答案:

答案 0 :(得分:1)

看起来这里有两个问题:

1)你的函数被称为“statusShowHide()”,而不是“showHide()”,所以这可能是它没有被调用的原因。 2)你的onchange属性在函数调用之后缺少它的结束引号,所以也可以这样。

给这些修补程序一个镜头,看它现在是否有效。

编辑:还有一些建议:

在你的showHide()方法中你说:

if ( (status == 3) && (cboStatusvalue == 'Open' )

应该是:

if ( (cboStatus === 3) && (cboStatusValue === 'Open' ) )

此外,而不是您当前获得元素值的方式:

var cboStatusValue = cboStatus.options[cboStatus.selectedIndex].text;

尝试使用:

var cboStatusValue = cboStatus.value;

答案 1 :(得分:1)

最终解决了以下问题:

填充初始记录值的窗口onload:

window.onload = findStatus;

    function findStatus()   
    {
        var cboStatus = document.getElementById('cboStatus');
        statusShowHide(cboStatus);      
    }

关注此Javascript以更改状态:

function statusShowHide(obj)
{   
    var txtStatusFirstLoad = document.getElementById("txtStatusFirstLoad");                  
    var lbReopenReason = document.getElementById("lbReopenReason");
    lbReopenReason.style.display = "none";

    if (txtStatusFirstLoad.value == 3 && obj.value == 1)
    {

        lbReopenReason.style.display = "block";
    }   
}

并添加了此隐藏文本框以捕获初始状态值:

<input name="txtStatusFirstLoad" id = "txtStatusFirstLoad" type="hidden" value="<%=RS("Status")%>" />
相关问题