簡體   English   中英

禁用鏈接,直到輸入和選擇字段具有值

[英]Disable Link Until Input and Select Fields Have Values

我希望確保所有下拉字段均具有選定值,而輸入字段均具有值。 我該怎么做呢? 提前致謝。

$(document).ready(function() {
validate();
$('input').on('keyup', validate);
});

function validate() {
var inputsWithValues = 0;
var myInputs = $("input:not([type='hidden'])");

myInputs.each(function(e) {
// if it has a value, increment the counter
if ($(this).val()) {
  inputsWithValues += 1;
}
});

if (inputsWithValues == myInputs.length) {
$(“.button_link").removeClass('disabled').addAttr("href"); 
} else {
$(“.button_link").addClass('disabled').removeAttr("href");
}
}

我正在尋找如何檢測兩個輸入字段和選擇項? 我的代碼僅在部分選擇上有效。

var myInputs = $(“ input:not([type ='hidden'])”);

(選擇)???

您可以嘗試這樣的事情。 您可能需要稍微調整一下。

$(document).ready(function() {
    //validate();
    $('input').on('blur', function(){
        if(validate()){
            $(".button_link").prop('disabled', false);
        } else {
            $(".button_link").prop('disabled', true);
        }
    });
    $('select').on('change', function(){
        if(validate()){
            $(".button_link").prop('disabled', false);
        } else {
            $(".button_link").prop('disabled', true);
        }
    });
});

function validate() {
    $("input:not([type='hidden'],[type='button'])").each(function(){
        if($(this).val() == ""){
            return false;
        }
    });

    $("select").each(function(){
        if($(this).val() == ""){ // change empty string if unselected has a value (e.g. 0)
            return false;
        }
    });

    return true;
}

好的,我終於可以正常工作了,如果有人對此有任何調整,請告訴我。

<script>
$(document).ready(function() {
validate();
$('input').on('keyup', validate);
$('select').on('change', validate);
});

function validate() {
var inputsWithValues = 0;

// get all input fields except for type='submit'
// var myInputs = $("input:not([type='submit'])");
var myInputs = $("option:selected, input:not([type='hidden'])");

myInputs.each(function(e) {
// if it has a value, increment the counter
if ($(this).val()) {
  inputsWithValues += 1;
}
});

if (inputsWithValues == myInputs.length) {
$(".button_link").removeClass('disabled').addAttr("href"); 
} else {
$(".button_link").addClass('disabled').removeAttr("href");
}
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM