【问题标题】:How to set required field in contact form 7 wordpress如何在联系表格7 wordpress中设置必填字段
【发布时间】:2018-09-17 23:39:17
【问题描述】:

我在表单中有一个包含多个复选框的列表。用户可以选中所有复选框,但至少需要一个。如果不选择至少一个复选框,他们应该无法提交表单。如何在联系表格 7 中填写? 我添加了不起作用的脚本。

这是我的脚本

$(文档).ready(函数 () { $('#checkBtn').click(function() {

checked = $("input[type=checkbox]:checked").length;

 if(!checked) {
    alert("You must check at least one checkbox.");
    return false;
  }

});

});

【问题讨论】:

  • 请向我们展示您的脚本
  • @HarshMehta 添加了上面的脚本,但仍然没有使用该脚本。

标签: wordpress contact-form-7


【解决方案1】:

这里只在插件表单中的复选框元素之后写星(*)

 [checkbox* checkbox "test 1" "test 2"]

 [submit "Send"]

【讨论】:

  • 我正在添加单独的复选框,例如 [checkbox checkbox-123 "10"] [checkbox checkbox-124 "11"]。所以在这里至少他们应该选择任何一个。
  • 这也是可行的。添加星号(*)你想要的字段。
  • 是的,它会起作用的。但在这里,用户可以根据需要选择任何一个和 2 个或更多。我有 7 个单独的复选框的列表。如果我们添加星号(*),那么用户应该选择所有复选框。在这 7 个复选框中,用户可以选择任何一个或两个或更多。这取决于用户。但在这里我至少需要他们应该选择任何一个复选框。
  • 好的。我添加了上面的脚本,但它没有接受联系表格 7. 在 HTML 表单中,该脚本工作正常。
  • 我只在 HTML 中创建了演示。这是所有工作的地方
【解决方案2】:

这是完美的工作。 在此设置您的代码

$('#fm_submit').submit(function(e){
    e.preventDefault();
    var ck_box = $('input[type="checkbox"]:checked').length;
    
    // return in firefox or chrome console 
    // the number of checkbox checked
    console.log(ck_box); 

    if(ck_box < 1){
      alert('Select any one box');
    } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name = "frmTest[]" id="fm_submit">
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" >
  <input type="checkbox" >
  <input type="submit" id="fm_submit" name="fm_submit" value="Submit">
</form>
<div class="container"></div>

【讨论】:

  • 我在附加设置选项卡内的联系表格 7 插件中添加的上述脚本。它不接受脚本。我在哪里可以在联系表格 7 中添加该脚本?
  • 所以 "$('#fm_submit').submit(function(e){" 在 'click' 的地方提交 "$('#fm_submit').click(function(e){"
  • 脚本未采用联系表格 7。我尝试在联系表格 7 的表格标签中添加一个额外的设置选项卡和该表格下方。两个地方都没有采用该脚本。
  • 请检查你的js连接。
【解决方案3】:

将上面的代码粘贴到表单底部,而不是附加设置

$('#fm_submit').submit(function(e){
e.preventDefault();
var ck_box = $('input[type="checkbox"]:checked').length;
// return in firefox or chrome console the number of checkbox checked enter code here
console.log(ck_box); 
if(ck_box < 1){ alert('Select any one box'); } 
});

【讨论】:

  • 代码不会在附加设置中执行。它需要插入到表单部分的底部。
猜你喜欢
  • 1970-01-01
  • 2017-08-06
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多