排除从属列表

时间:2017-07-12 22:03:44

标签: javascript jquery html

我正在尝试创建一个下拉列表,其中范围编号将取决于cm名称。我已经尝试过并尝试了我的代码,每次运行它时,我都可以选择cm名称,但是范围编号框不允许我选择任何范围编号。有什么建议吗?

HTML代码

<body>
  <form action="submitform.asp" method="post" name="tipinvestmentsemiannualprogressform " id="tipinvestmentsemiannualprogressform ">
    <h2>TIP Investment Semi-Annual Progress Form</h2>
    <table width="50% " border="0 " cellspacing="0 " cellpadding="5 ">
      <tr>
        <td width="41% " align="right " valign="middle ">CM Name :</td>
        <td width="59% " align="left " valign="middle ">
          <select name="cmname " id="cmname ">
        <option value=" ">Select CM Name</option>
        <option value="p_c ">P C</option>
        <option value="g_g ">G G</option>
        <option value="b_h ">B H</option>
        <option value="b_h ">S H</option>
        <option value="k_k ">K K</option>
        <option value="j_l ">J L</option>
        <option value="e_l ">E L</option>
        <option value="r_t ">R T</option>
        </select>
        </td>
      </tr>
      <tr>
        <td align="right " valign="middle ">Scope Number :</td>
        <td align="left " valign="middle ">
          <select disabled="disabled " id="scopenumber " name="scopenumber ">
        <option class="label " value>Select Scope Number</option>
        <!-- P C -->
        <option rel="p_c " value="black ">black</option>
        <!-- G G-->
        <option rel="g_g " value="red ">red</option>
        <!-- B H -->
        <option rel="b_h " value="orange ">orange</option>
        <!-- S H -->
        <option rel="s_h " value="yellow ">yellow</option>
        <!-- K K -->
        <option rel="k_k " value="green ">green</option>
        <!-- J L -->
        <option rel="j_l " value="blue ">blue</option>
        <!-- E L -->
        <option rel="e_l " value="indigo ">indigo</option>
        <!-- R T -->
        <option rel="r_t " value="violet ">violet</option>
        </select>
        </td>
      </tr>
    </table>
  </form>
</body>

JS

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
  $(function()) {

  var $cat = $("#cmname"),
    $subcat = $("#scopenumber");

  $cat.on("change", function()) {

    var _rel = $(this).val();

    $subcat.find("option").attr("style", "");
    $subcat.val("");

    if (!_rel) return $subcat.prop("disabled", true);

    $subcat.find("[rel=" + _rel + "]").show();
    $subcat.prop("disabled", false);
  });

  });
</script>

1 个答案:

答案 0 :(得分:-1)

我想你想改变这一行:

$subcat.find("[rel=" + _rel + "]").show();

为:

$subcat.find("[rel=" + _rel + "]").prop('selected', true);