根据 select 选项值禁用文本输入

Disable text input based on select option value

在下面的代码中,如果 select 选项值为 0,我想禁用文本输入。怎么了?

$('select').change(function() {
  if ($(this).val() == 0)) (".xyz").attr('disabled',true);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="type">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<br />
<input type="text" class="xyz" name="xyz" />

您的代码中存在语法错误:

Uncaught SyntaxError: Unexpected token )

您将 $(jQuery 选择器)拼错为 )

此外,您还没有处理 "type" select 具有非 0 值的情况。

$("select").change(function() {
  if ($(this).val() == 0) {
    $(".xyz").attr("disabled", "disabled");
  } else {
    $(".xyz").removeAttr("disabled");
  }
}).trigger("change");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="type">
  <option value="0"></option>
  <option value="1">1</option>
  <option value="2">2</option>
</select>
<br />
<input type="text" class="xyz" name="xyz" />