php - 如何检查任何文本区域, radio 被检查jquery

标签 php jquery

我需要使用 jQuery 验证表单。我可以检查我的表单是否为空。在我的表单中,我可以有不同类型的输入元素:文本区域、单选按钮等

我有保存按钮

表单不能以空值提交。但如果选中任何一个文本区域或任何一个复选框(不强制检查所有值并填充所有文本区域,但表单不能为空)

对 jquery 非常陌生,对我来说非常复杂:(我尝试过

function checkValues() {
  if ($.trim($(this).val()) == '') {
    alert('not okay');
    return false;
  } else {
    alert('okay');
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="submit" name="save" value="Open" onClick="checkValues()" class="btn btn-primary">Save</button>

如有任何帮助,我们将不胜感激。

最佳答案

我建议您使用表单的提交事件:

这里测试为空 - 如果填充/检查/选择任何内容,它将允许提交

$("#form1").on("submit", function(e) {
  var empty = true;
  $(":input").each(function() { // all types of form fields
    if (this.type.indexOf("text") == 0 || this.type == "password") { // text, textarea and password
      empty = $.trim(this.value) == "";
      if (!empty) return false; // leave the each
    } else if (this.type.indexOf("select") == 0) { // select-one/multiple
      empty = this.value == "";
      if (!empty) return false; // leave the each
    } else if (this.type == "checkbox") {
      empty = !this.checked
      if (!empty) return false; // leave the each
    }
  });
  // radios are a special case
  if (empty) { // only bother testing if nothing is filled
    var $radios = $("[type=radio]",this); // all radios in the form
    if ($radios.length > 0) { // any radios?
      empty = $("[type=radio]:checked", this).length==0; // any checked?
    }
  }  
  if (empty) {
    console.log("All fields are empty")
    e.preventDefault(); // cancels the submission
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1">
  <input type="text" value="" /><br/>
  <textarea></textarea><br/>
  <label>OK? <input type="checkbox" value="OK"/></label><br/>
  <label>Yes <input type="radio" name="rad" value="yes"/></label>
  <label>No  <input type="radio" name="rad" value="no"/><br/></label>
  <select>
    <option value="">Please select</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select><br/>
  <input type="submit" />
</form>

或者尝试这个,它会计算填充值

$("#form1").on("submit", function(e) {
  var values = [],
    val = "";
  $(":input").each(function() { // all types of form fields
    if (this.type.indexOf("text") == 0 || this.type == "password") { // text, textarea and password
      val = $.trim(this.value);
      if (val !== "") values.push(val);
    } else if (this.type.indexOf("select") == 0) { // select-one/multiple
      if (this.value !== "") values.push(this.value);
    } else if (this.type == "checkbox") {
      if (this.checked) values.push(this.value);
    }
  });
  var $radios = $("[type=radio]", this); // all radios in the form
  if ($radios.length > 0) { // any radios?
    var $checked = $("[type=radio]:checked", this);
    if ($checked.length > 0) { // any checked?
      $checked.each(function() { values.push(this.value); })
    }
  }
  if (values.length > 0) {
    console.log(values);
    e.preventDefault(); // cancels the submission    
  } else {
    console.log("All fields are empty")
    e.preventDefault(); // cancels the submission
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1">
  <input type="text" value="" /><br/>
  <textarea></textarea><br/>
  <label>OK? <input type="checkbox" value="OK"/></label><br/>
  <label>Yes <input type="radio" name="really" value="yes"/></label>
  <label>No  <input type="radio" name="really" value="no"/><br/></label>
  <select>
    <option value="">Please select</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select><br/>
  <label>Left <input type="radio" name="direction" value="left"/></label>
  <label>Right  <input type="radio" name="direction" value="right"/><br/></label>
  <input type="submit" />
</form>

关于php - 如何检查任何文本区域, radio 被检查jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52549323/

相关文章:

php - 如何在安装 Moodle block 期间运行某些功能?

php - Google/YouTube Insight 样式 "Interactive PNGs"?

javascript - 触发选择框的 onChange 事件(Jquery Chosen)

jQuery 无法与 SemanticUI 和 Electron.js 一起使用

jQuery 调用 CSS3 淡入淡出动画?

c# - 应用程序可以在 Unity 编辑器中运行,但不能在 Android 上运行

Php 向输入字段添加值?

jquery - 在不重新下载图像的情况下在弹出窗口中 Bootstrap 图像

php - 在 PHP 中内部加入 mongoDB

javascript - 如何使用 jQuery 追加/前置/创建文本节点