【问题标题】:Disable text input based on select option value根据选择选项值禁用文本输入
【发布时间】:2018-07-21 04:22:04
【问题描述】:

在下面的代码中,如果选择选项值为 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" />

【问题讨论】:

    标签: jquery disabled-input


    【解决方案1】:

    您的代码中有语法错误:

    未捕获的SyntaxError:意外令牌)

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

    此外,您还没有处理“类型”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" />

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2016-12-21
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多