单击HTML选择的父级

J D*_*oe. 2 html javascript jquery

select当它被禁用时,我似乎无法单击a的父级.我试图让用户通过点击来解锁输入,但它只适用于inputs.

    let $input = $('input')
    $input.parent().click(function() {
      $input.prop('disabled', false);
    })
    let $select = $('select')
    $select.parent().click(function() {
      $select.prop('disabled', false);
    })
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="parent">
      <input name="name" disabled placeholder="click me">
    </div>
    <div class="parent">
      <select name="thing" disabled>
        <option value="1">1</option>
      </select>
    </div>
Run Code Online (Sandbox Code Playgroud)

Ric*_*ock 5

添加此样式:

select[disabled] {
  pointer-events: none;
}
Run Code Online (Sandbox Code Playgroud)

这将导致禁用选择忽略鼠标,这允许其父级捕获点击.

片段:

let $input = $('input');
$input.parent().click(function() {
  $input.prop('disabled', false);
});

let $select = $('select');
$select.parent().click(function() {
  $select.prop('disabled', false);
});
Run Code Online (Sandbox Code Playgroud)
select[disabled] {
  pointer-events: none;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
  <input name="name" disabled placeholder="click me">
</div>
<div class="parent">
  <select name="thing" disabled>
    <option value="1">1</option>
  </select>
</div>
Run Code Online (Sandbox Code Playgroud)