简体   繁体   English

内容页面中带有警报的Javascript禁用按钮

[英]Javascript disable button with alert in content page

I have to prepare a small script that can disable button if input field is lower than number 1, and when the user presses the button while it is disabled he will get alert. 我必须准备一个小的脚本,如果输入字段的数字小于数字1,可以禁用按钮,并且当用户在禁用按钮时按下按钮时,他会收到警报。 Alert should be in content page, no popup. 警报应该在内容页面中,没有弹出窗口。 I prepared something like that: 我准备了这样的东西:

<!-- language: lang-js --> 
var toValidate = $('#inputName, #inputEmail, #inputTel'),
valid = false;
toValidate.keyup(function () {
 if ($(this).val().length > 0) {
   $(this).data('valid', true);
 } else {
   $(this).data('valid', false);
}
toValidate.each(function () {
 if ($(this).data('valid') == true) {
   valid = true;
 } else {
 valid = false;
 }
});
 if (valid === true) {
  $('input[type=submit]').prop('disabled', false);
 }else{
  $('input[type=submit]').prop('disabled', true);        
 }
});

This code works very well, but it only works if there is any value in the input field. 该代码可以很好地工作,但是只有在输入字段中有任何值时,它才有效。 I can enter 0 and validation is passed, also i can't modify the code to display alert text if user click on disable button. 我可以输入0并通过验证,如果用户单击“禁用”按钮,我也无法修改代码以显示警报文本。

<input type="text" />
<input type="submit" value="Button" />
<div></div>

here is CSS : 这是CSS:

.disabled {
    background-color: #DDD;
    color: #999;
}

here is the script : 这是脚本:

    function Validate(value) {
    if (value.length === 0) {
        $("input[type='submit']").addClass('disabled');
        return;
    } else $("input[type='submit']").removeClass('disabled');
}

$(document).ready(function () {
    $("input[type='submit']").hasClass('disabled');

    $("input[type='text']").keyup(function () {
        Validate($("input[type='text']").val());
    });

    $("input[type='submit']").click(function () {
        Validate($("input[type='text']").val());
        if ($(this).hasClass('disabled')) $("div").html("Alert msg");
        else return;
    });
});

Try this 尝试这个

HTML 的HTML

<input type="text" id="vall" />
<input type="submit" id="btn" class="disabled" value="Button" />
<div></div>

Script 脚本

var value=$('#vall').val();

$("#vall").keyup(function(){

    if (value.length == 0) 
    {
      $("#btn").addClass('disabled');
        $("div").html("Fill Value");
    } 
    else 
        $("#btn").removeClass('disabled');
});
   $("#btn").click(function (){
        if (value.length == 0)
            $("div").html("Fill Value");
       else
           $("div").html("");
    });

CSS 的CSS

.disabled, .btn[disabled] {
    background-image: none;
    box-shadow: none;
    cursor: default;
    opacity: 0.65;
}

Fiddle 小提琴

http://jsfiddle.net/9asLF/2/ http://jsfiddle.net/9asLF/2/

Here is my stab at it. 这是我的目的。 I know it has already been answered... but I already did the work. 我知道已经回答了...但是我已经完成了工作。

JSFIDDLE JSFIDDLE

http://jsfiddle.net/4JHy2/ http://jsfiddle.net/4JHy2/

HMTL HMTL

<ul class='errors' id='errors'></ul>

    <form id='myForm'>
        <label for'inputName'>Name:</label><br>
        <input type='text' id='inputName'>

        <br><br>

        <label for'inputEmail'>Email:</label><br>
        <input type='text' id='inputEmail'>

        <br><br>

        <label for'inputTel'>Telephone:</label><br>
        <input type='text' id='inputTel'>

        <br><br>

        <input type='submit' value='Submit' id='submit'>
    </form>

Javascript Java脚本

var regExp = /^$|^ +$/,
        elements = [],
        eLength,
        formValid = false,
        errors;

    function clearErrors() { errors.html('') }

    function addError() { errors.append($('<li>Form disabled until all fields complete.</li>')) }

    $(function(){
        // Cache the form elements for performance and loopage.
        elements.push($('#inputName'));
        elements.push($('#inputEmail'));
        elements.push($('#inputTel'));
        eLength = elements.length;
        errors = $('#errors');

        // Listen for activity on all three elements.
        $('#inputName, #inputEmail, #inputTel').on('keyup', function(){
            // Clear previous errors.
            clearErrors();

            for(var i = 0; i < eLength; i++) {
                if (regExp.test($(elements[i]).val())) {
                    formValid = false;
                } else {
                    formValid = true;
                }
            }

            if (!formValid) { 
                addError();
            }
        });

        $('#myForm').on('submit', function(e){
            e.preventDefault();

            // Clear previous errors.
            clearErrors();

            if (formValid) {
                alert("valid");
            } else {
                addError();
            }
        });
    });

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

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