0

在这段代码中,我们需要在状态(上方字段)中选择 APPROVED 时禁用 Department 字段,并且在选择 CLARIFICATIONS 时应该启用它必须启用。请尝试帮助我们!

<form name="index" action="Director">
    <table border="0"  style="width:300px">
        <tbody bgcolor>
            <p> Date: <%= df.format(new java.util.Date()) %> </p>
            <tr>
                <td height="40"><font color="Blue">Branch:</td>
                <td>

                    <select name="dbranch" id="dbranch" >
                        <option value="Select">Select</option>
                        <option value="Kakinada">Kakinada</option>
                        <option value="Vishakapatnam">Vishakapatnam</option>
                        <option value="Tirupathi">Tirupathi</option>
                        <option value="Vijayawada">Vijayawada</option>
                        <option value="Warangal">Warangal</option>
                        <option value="Hyderabad">Hyderabad</option>
                    </select>
                </td>
            </tr>                                
            <tr>
                <td height="40"><font color="Blue">Doc id :</td>
                <td>
                <select id="inw_id" name="inw_id">
                <option value='0'>Select Doc ID</option>
                </select>
                </td>
            </tr>
            <tr>
                <td height="40"><font color="Blue">Status:   </td>
                <td>
                    <select name="status" id="status" >
                        <option value="">Status</option>
                        <option value="Approved">Approved</option>
                        <option value="Clarifications">Clarifications</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td height="40"><font color="Blue">Department:   </td>
                <td>
                    <select name="dept" id="dept" >
                        <option value=""> </option>
                        <option value="Admin">Admin</option>
                        <option value="Accounts">Accounts</option>
                        <option value="Other">Other</option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>
    <tr><td><td align="right"><input name="submit" type="submit" value="Submit"style="background-color:LightBlue;margin-left:15px;height: 40px;width: 100px; margin-right: -30px;"></td></td></tr>
</form>

我们尝试编写一个函数,但它不起作用:(

function doenable() {
    if(document.getElementById("status")!="null") {
        document.getElementById("department").disabled=false;
    }
}
4

3 回答 3

0

如果您想要纯 JavaScript 解决方案,请尝试以下操作。

<html>

<head>
  <script type='text/javascript'>
    function change(theStatus) {
      if (theStatus.value === "Approved") {
        document.getElementById("dept").disabled = true;
      } else {
        document.getElementById("dept").disabled = false;
      }
    }
  </script>
</head>

<body>
  <select name="status" id="status" onchange="change(this)">
    <option value="">Status</option>
    <option value="Approved">Approved</option>
    <option value="Clarifications">Clarifications</option>
  </select>
  <select name="dept" id="dept">
    <option value=""></option>
    <option value="Admin">Admin</option>
    <option value="Accounts">Accounts</option>
    <option value="Other">Other</option>
  </select>
</body>

</html>

于 2013-05-13T19:52:19.417 回答
0

尝试在选择字段中设置 onchange 功能。尝试这样做:

<select name="status" id="status" onchange="doenable()" >
  <option value="">Status</option>
  <option value="Approved">Approved</option>
  <option value="Clarifications">Clarifications</option>
</select>

和你的javascript函数:

function doenable() {
  if(document.getElementById("status").value != "" {
    document.getElementById("department").enabled = false;
  } else {
    document.getElementById("department").enabled = true;
  }
}

如果我是你,我会使用 jQuery,它比普通的 js 更简单更好。

于 2013-05-13T19:02:46.567 回答
0

我重写了你的代码。在这里,工作。尝试这样做:

JS

  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script>
    $().ready(function(){                
      $('#dept').prop('disabled', true);
    });

    function doenable(dept_value) {
      alert(dept_value);
      if(dept_value == '') {
        $('#dept').prop('disabled', true);
      } else {
        $('#dept').prop('disabled', false);
      }
    }
  </script>

HTML

<form name="index" action="Director">
    <table border="0"  style="width:300px">
        <tbody bgcolor>

            <tr>
                <td height="40"><font color="Blue">Branch:</td>
                <td>

                    <select name="dbranch" id="dbranch" >
                        <option value="Select">Select</option>
                        <option value="Kakinada">Kakinada</option>
                        <option value="Vishakapatnam">Vishakapatnam</option>
                        <option value="Tirupathi">Tirupathi</option>
                        <option value="Vijayawada">Vijayawada</option>
                        <option value="Warangal">Warangal</option>
                        <option value="Hyderabad">Hyderabad</option>
                    </select>
                </td>
            </tr>                                
            <tr>
                <td height="40"><font color="Blue">Doc id :</td>
                <td>
                <select id="inw_id" name="inw_id">
                <option value='0'>Select Doc ID</option>
                </select>
                </td>
            </tr>
            <tr>
                <td height="40"><font color="Blue">Status:   </td>
                <td>
                    <select name="status" id="status" onchange="doenable(this.value)">
                        <option value="">Status</option>
                        <option value="Approved">Approved</option>
                        <option value="Clarifications">Clarifications</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td height="40"><font color="Blue">Department:   </td>
                <td>
                    <select name="dept" id="dept" >
                        <option value=""> </option>
                        <option value="Admin">Admin</option>
                        <option value="Accounts">Accounts</option>
                        <option value="Other">Other</option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>
    <tr><td><td align="right"><input name="submit" type="submit" value="Submit"style="background-color:LightBlue;margin-left:15px;height: 40px;width: 100px; margin-right: -30px;"></td></td></tr>
</form>
于 2013-05-13T19:35:47.337 回答