如果其他字段包含文本,则阻止文本字段


Block text fields if others contain text?

如果有办法(最好是jQuery)阻止文本输入,如果其他人包含文本?

我可以看到一直阻止输入字段的方法,但不是动态的,因为其他字段包含文本。

<input name="inputone" id="i_one" type="text" value="">
<input name="inputtwo" id="i_two" type="text" value="">
<input name="inputthree" id="i_three" type="text" value="">

这是一个非常简单的设置。我只需要如果"i_one"包含任何文本,阻止"i_two"和"i_three"接收任何输入。但是,如果用户在提交表单之前删除了他们在"i_one"中键入的内容,则阻止将被解除,以便用户可以改用其他字段。

这个样子怎么样?

$('input').on('keyup', function() {
  $(this).siblings().prop('disabled', this.value.length ? true : false)
});

当然,您可能希望通过向要侦听的元素添加一个特殊类来调整它以不对所有输入元素进行操作。

JsFiddle如果你想调整/玩它。

我很快就想出了这个。

$(document).ready(function() {
    $("input").on("input", function() {
        if ($(this).val() == "") {
            $("input").removeAttr("disabled");
        }
        else {
            $("input").attr("disabled", "disabled");
            $(this).removeAttr("disabled");
        }
    });
});