简体   繁体   English

仅在提供两个条件的情况下,如何启用/禁用提交按钮:检查输入单选和填充输入文本

[英]how to enable/disable a submit button, only if two conditions are provided: checking input radio and filling input text

I have this form: 我有这个表格:

<form name="myform" method="post">
  <input class="class_x" type="radio" name="name_1" value="value_1" id="id_1"/><br/>
  <input class="class_x" type="radio" name="name_2" value="value_2" id="id_2"/><br/>
  <input class="class_x" type="radio" name="name_3" value="value_3" id="id_3"/><br/>
  <input onkeyup="checkFormsValidity();" id="input_id" type="text" name="input_name" value="" required/><br/>
  <input type="submit" name="submit_name" value="OK" class="submit_class" id="SubmitButton"/>
</form>

three input radios, one input text, and one input submit button. 三个输入收音机,一个输入文本和一个输入提交按钮。

According to the javascript code: 根据javascript代码:

//ENABLE OR DISABLE SUBMIT BUTTON CODE ACCORDING TO INPUT RADIO
function checkIfValid() {
var current = $('input.class_x').filter(':checked');

  if (current.length > 1) {
    sbmtBtn.disabled = false;
  } else {
    sbmtBtn.disabled = true;
  }
};

//ENABLE OR DISABLE SUBMIT BUTTON CODE ACCORDING TO INPUT TEXT
checkFormsValidity = function(){
var myforms = document.forms["myform"];   
    if (myforms.checkValidity()) {
        sbmtBtn.disabled = false;
    } else {
        sbmtBtn.disabled = true;
    }
}

if any of these two conditions are provided the input submit button will be enabled. 如果提供了这两个条件中的任何一个,则将启用输入提交按钮。 BUT what I need is that the input submit button ONLY enables if BOTH conditions are provided . 但是我需要的是, 只有同时提供两个条件 ,输入提交按钮才启用 AND if ANY of these conditions are changed back THEN the submit button should disable back again. 并且,如果这些条件中的任何一个被更改,则提交按钮应再次禁用。

I know this needs a very strict syntax. 我知道这需要非常严格的语法。

According to the code I am providing, what changes need to be done in order to achieve this? 根据我提供的代码,要实现此目的需要进行哪些更改? Here is the JSFIDDLE https://jsfiddle.net/Suiberu/1n5gbooq/ 这是JSFIDDLE https://jsfiddle.net/Suiberu/1n5gbooq/

thanks! 谢谢!

Try this: Updated fiddle 试试这个: 更新小提琴

condition : select with 2 radio button & one text input . 条件:用2 radio button & one text input

I was remove the link keyup event in html added direct with javascript. 我正在删除直接用javascript添加的html中的链接keyup event No need to separably validate the input text 无需单独验证input text

 //SUBMIT BUTTON VARIABLE DECLARED var sbmtBtn = document.getElementById("SubmitButton"); sbmtBtn.disabled = true; //INPUTS RADIO; CHECK OR UNCHECK var prv; var markIt = function(e) { if (prv === this && this.checked) { this.checked = false; prv = null; } else { prv = this; } checkIfValid(); }; $(function() { $('input.class_x').on('click', markIt); $('input[type=text]').on('keyup', markIt); }); //ENABLE OR DISABLE SUBMIT BUTTON CODE ACCORDING TO INPUT RADIO function checkIfValid() { var current = $('input.class_x').filter(':checked'); if ((current.length > 1) && ($('input[type=text]').val() != "")) { sbmtBtn.disabled = false; } else { sbmtBtn.disabled = true; } }; 
 input[type='submit']:disabled{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="myform" method="post"> <input class="class_x" type="radio" name="name_1" value="value_1" id="id_1"/><br/> <input class="class_x" type="radio" name="name_2" value="value_2" id="id_2"/><br/> <input class="class_x" type="radio" name="name_3" value="value_3" id="id_3"/><br/> <input id="input_id" type="text" name="input_name" value="" required/><br/> <input type="submit" name="submit_name" value="OK" class="submit_class" id="SubmitButton"/> </form> 

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

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