简体   繁体   English

仅当所有字段都已填写时,如何接受值或启用按钮?

[英]How to accept values or enable a button only if all fields are filled?

I trying to find a way to accept values from my HTML (razorview) on a button click or by pressing "Enter".我试图找到一种方法,通过单击按钮或按“Enter”来接受来自我的 HTML(razorview)的值。 What I want to do is我想做的是

  1. Enable the send button only if both the fields are filled up, else it should be disabled.仅当两个字段都已填写时才启用发送按钮,否则应禁用它。
  2. Send values by pressing "Enter" key.按“Enter”键发送值。 But, this should also happen only if both fields are filled up.但是,这也应该仅在两个字段都已填写时发生。

My initial (failed) attempts look somewhat like:我最初的(失败的)尝试看起来有点像:

 < script src = "~/Scripts/jquery.signalR-2.2.2.min.js" > < /script> < script src = "~/signalr/js" > < /script> < script src = "~/Scripts/SignalR.js" > < /script> < script type = "text/javascript" src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" > < /script> < script type = "text/javascript" > < /script> (function() { var myHub = $.connection.myHub; $("#textarea").keyup(function() { if ($(this).val() != "" && $("textarea").val() != "") { console.log("This should not run unless all feilds are full."); var text = ($(this).val()).toString(); text = text.replace(/\\s+/g, " ").trim(); var message = ($("textarea").val()).toString(); message = message.replace(/\\s+/g, " ").trim(); $('input[type="button"]').removeAttr('disabled'); } else { $("#send").attr('disabled', 'disabled'); } }); $('#yourMessage, fname').keyup(function(e) { if ($("#yourMessage").val() != "" && $("#fname").val() != "") { if (e.which == 13) { //Enter key pressed $('#send').trigger('click'); //Trigger search button click event } } }); })();
 .button { text-decoration: none; border: none; padding: 12px 20px; cursor: pointer; outline: 0; display: inline-block; margin-right: 2px; border-radius: 12px; background-color: blue; opacity: 1; } .button-blue { background-color: #3498db; } .button-blue:hover { background-color: #3498db; } .button-blue:active { color: #3498db; background-color: #3498db; box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.2); } .button:disabled { opacity: 0.6; pointer-events: none; }
 <h1>Hello!</h1> <form action="" method="post" id="subscribe" name="subscribe"> First name: <input type="text" name="fname" id="fname" placeholder="Name" required autofocus><br> <div id="message"></div> Your Message:<br> <textarea typeof="text" id="yourMessage" placeholder="Start Typing..." required></textarea><br /> <input type="button" class="button button-blue" value="Send" name="button" disabled="disabled" id="send" /> </form> <input type="button" class="button button-blue" value="Click Me!" id="click-me" />

The answer is you have to bind to all the different cases.答案是您必须绑定到所有不同的情况。 Here is a working jsfiddle of what you're looking for: https://jsfiddle.net/4z5zgqbn/这是您正在寻找的工作jsfiddle: https ://jsfiddle.net/4z5zgqbn/

$(document).ready(function() {
  $("#send").prop("disabled", true);

  $("#yourMessage").on("keyup", function() {
    if ($(this).val() != "" && $("#fname").val() != "") {
      $("#send").prop("disabled", false);
    } else {
      $("#send").prop("disabled", true);
    }
  });
  $("#fname").on("keyup", function() {
    if ($(this).val() != "" && $("#yourMessage").val() != "") {
      $("#send").prop("disabled", false);
    } else {
      $("#send").prop("disabled", true);
    }
  });

  $("#yourMessage, #fname").on("keyup", function(e) {
    if (e.which == 13 && $("#fname").val() != "" && $("#yourMessage").val() != "") {
      $("#send").click();
    }
  })

  $("#send").on("click", function() {
    alert("send message");
  })
})

To 1: Thats what I have done: give all your inputs one same class name (.classname), To 1:这就是我所做的:给你所有的输入一个相同的类名(.classname),

$(".classname").bind("change paste keyup", function() {
    if ($("#fname").val() && $("#yourMessage").val()) { 
        $( "#Send" ).prop( "disabled", false ); 
    } else {
        $( "#Send" ).prop( "disabled", true );
    }
});

To 2:到 2:

$('.classname').on('keypress', function (e) {
     if(e.which === 13 && $("#fname").val() && $("#yourMessage").val()){

        //Disable textbox to prevent multiple submit
        $( "#Send" ).prop( "disabled", true );
        //Submit your form
     }

}); });

haven´t testet it.还没有测试它。

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

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