简体   繁体   English

禁用元素,直到其他输入字段有效

[英]Disable an element until other input fields are valid

I am working on an HTML5 form with javascript validation. 我正在使用带有JavaScript验证的HTML5表单。 I would like to disable a select element until several input boxes contain valid data. 我想禁用一个选择元素,直到几个输入框包含有效数据。 Here's my simplified HTML: 这是我的简化HTML:

<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num1" required onchange="valid(this)" />
<br />
<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num2" required onchange="valid(this)" />
<br />
<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num3" required onchange="valid(this)" />
<br />
<select id="drop" disabled>
    <option>disabled until input boxes are valid</option>
    <option>A</option>
    <option>B</option>
    <option>C</option>
</select>

Here's the simplified javascript: 这是简化的javascript:

function valid(obj) {
    if ((obj.validity) && (!obj.validity.valid)) {
        return false;
    } else {
        return true;
    }
}

var numA = document.getElementById("num1"),
    numB = document.getElementById("num2"),
    numC = document.getElementById("num3");

if (valid(numA) && valid(numB) && valid(numC)) {
    document.getElementById("drop").disabled = false;
}

and here's my jsfiddle 这是我的jsfiddle

Can this be accomplished with plain 'ol javascript? 可以使用普通的'ol javascript吗? As I am still trying to learn Javascript, I am not yet familiar with JQuery. 由于我仍在尝试学习Javascript,因此我还不熟悉JQuery。

Any time the input field content changes I would like to validate the input. 每当输入字段内容更改时,我都想验证输入。 Then it would need to check if all other input fields (num1, num2, num3) are valid. 然后,将需要检查所有其他输入字段(num1,num2,num3)是否有效。 If, and only if, all are valid it sets disabled = false on the select element. 当且仅当全部有效时,才在select元素上将disable设置为false。

Is there a better way to do this? 有一个更好的方法吗?

Try 尝试

<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num1" required onchange="check()" />
<br />
<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num2" required onchange="check()" />
<br />
<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num3" required onchange="check()" />

then 然后

function valid(obj) {
    //simplified version
    return !obj.validity || obj.validity.valid;
}


var numA = document.getElementById("num1"),
    numB = document.getElementById("num2"),
    numC = document.getElementById("num3");

function check() {
    document.getElementById("drop").disabled = !valid(numA) || !valid(numB) || !valid(numC);
}
check();

Demo: Fiddle 演示: 小提琴

Keep the script at the bottom of the page to make sure that when document.getElementById is executed the element will be already present in the dom. 将脚本保留在页面底部,以确保执行document.getElementById时,该元素将已经存在于dom中。 In jQuery you need to place the script in No Wrap - Body (Second dropdown in left panel) 在jQuery中,您需要将脚本放置在No Wrap - Body (左侧面板中的第二个下拉列表)

Here is answer for you. 这是给你的答案。 You need to check condition if all inputs are valid every time when you change input value. 每次更改输入值时,都需要检查条件是否所有输入都有效。

Here is edited version 这是编辑版本

HTML HTML

<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num1" required onchange="check()" />
<br />
<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num2" required onchange="check()" />
<br />
<input type="number" pattern="[0-9]{2}" min="1" max="100" id="num3" required onchange="check()" />
<br />
<br />
<select id="drop" disabled>
    <option>disabled until input boxes are valid</option>
    <option>A</option>
    <option>B</option>
    <option>C</option>
</select>

And here is JS 这是JS

function valid(obj) {
    if (obj.validity && !obj.validity.valid) {
        return false;
    } else {
        return true;
    }
}

    var numA = document.getElementById("num1"),
        numB = document.getElementById("num2"),
        numC = document.getElementById("num3");
function check(){

    if (valid(numA) && valid(numB) && valid(numC)) {
        document.getElementById("drop").disabled = false;
    }
}

Don't forget to check Nowrap - in head on JSfiddle 不要忘记检查Nowrap-在JSfiddle上

Here is your code simplified at jsFiddle 这是您在jsFiddle上简化的代码

This code is going to switch the state of your dropdown list to enabled if everything is valid, and back to disabled if the user turn the inputs to invalid. 此代码将在所有内容均有效的情况下将dropdown list的状态切换为enabled ,如果用户将输入变为无效,则将状态切换回disabled

JS: JS:

function valid(obj) {
    return !( obj.validity && !obj.validity.valid )
}

function testValidity() {
    document.getElementById("drop").disabled = !(
        valid( document.getElementById("num1") ) && 
        valid( document.getElementById("num2") ) && 
        valid( document.getElementById("num3") )
    );
}

HTML: HTML:

<input ... onchange="testValidity()" ... />

All the answers so far are right on, but in case you don't want to add the onchange attribute to each input here's an alternative: 到目前为止,所有答案都是正确的,但是如果您不想将onchange属性添加到每个输入中,则可以选择以下方法:

function valid(obj) {
    return !(obj.validity && !obj.validity.valid);
}

var inputs      = document.querySelectorAll('input'),
    inputLength = inputs.length,
    i;

var checkValidity = function() {
    var totalValid = 0;
    for (i = 0; i < inputLength; i++) {
        input = inputs[i];
        if (valid(input)) {
            totalValid += 1;
        }
    }

    document.getElementById('drop').disabled = (totalValid !== inputLength);
}

for (i = 0; i < inputLength; i++) {
    input = inputs[i];
    input.addEventListener('blur', checkValidity, false);
}

This just adds an blur event listener to each input and it dynamically changes the disabled on the dropdown depending on all the inputs validity. 这只会向每个输入添加一个模糊事件侦听器,并根据所有输入的有效性来动态更改下拉列表中的禁用状态。 If you eventually have the dropdown with a required attribute, you'll run into a bit of issues, so you'd probably want to remove that input from the inputs array. 如果最终有一个带有必填属性的下拉菜单,则会遇到一些问题,因此您可能希望从inputs数组中删除该输入。

Here's the jsfiddle: 这是jsfiddle: http://jsfiddle.net/EV88V/10/ http://jsfiddle.net/EV88V/10/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM