如何使用JQuery或Javascript将'required'添加到基于段落类的输入字段中?

时间:2019-04-26 16:51:42

标签: javascript jquery

由于某种原因(我相信这是一个很好的选择),Pardot不会在输入中添加“ required”,这将使访问者在单击“提交”按钮之前就知道该字段是否必要。点击提交按钮刷新我的页面并关闭我的弹出窗口。但是,在段落类中添加了“必需”。

是否可以循环遍历文本区域,输入,选择单词“ required”并将其添加到输入字段?

任何帮助将不胜感激。

		function validateForm() {
  var x = document.forms["pardot-form"]["company"].value;
  if (x == "") {
    alert("Please fill out the required ' * ' fields.");
    return false;
  }
}
<form accept-charset="UTF-8" action="" class="form row" id="pardot-form" method="post" name="pardot-form" onsubmit="return validateForm()">
 <p class="form-field company pd-text col-md-6">
  <label class="field-label" for="">Company Name</label>
  <input class="text" id="" maxlength="255" name="company" onchange="" size="30" type="text" value="">
 </p>
 <p class="form-field email pd-text required col-md-6">
  <label class="field-label" for="">Email Address</label>
  <input class="text" id="" maxlength="255" name="" size="30" type="text" value="">
 </p>
 <p class="form-field country pd-select required col-md-12">
  <label class="field-label" for="">Country</label>
  <select class="select" id="" name="" onchange="">
   <option selected="selected" value=""> </option>
   <option value="">United States</option>
   <option value="">Canada</option>
  </select>
 </p>
 <p class="form-field opted_out pd-checkbox required col-md-6">
  <label class="field-label" for="">Opt-out?</label>
  <span class="value"><span>
  <input id="" name="" onchange="" type="checkbox" value="">
  <label class="inline" for="">yes</label>
  </span></span> </p>
 <p class="form-field favorite_color pd-radio required col-md-6">
  <label class="field-label" for="">What is your favorite color?</label>
  <span class="value"><span>
  <input id="" name="" onchange="" type="radio" value="">
  <label class="inline" for="">blue</label>
  </span></span> </p>
 <p class="form-field comments pd-textarea required col-md-12">
  <label class="field-label" for="1">Tell us more.</label>
  <textarea class="standard" cols="40" id="" name="" onchange="" rows="10"></textarea>
 </p>
 <p style="position:absolute; width:190px; left:-9999px; top: -9999px;visibility:hidden;">
  <label for="pi_extra_field">Comments</label>
  <input id="pi_extra_field" name="pi_extra_field" type="text">
 </p>
 <!-- forces IE5-8 to correctly submit UTF8 content  -->
 <input name="_utf8" type="hidden" value="☃">
 <p class="submit col-md-12">
  <input accesskey="s" type="submit" value="Submit">
 </p>
</form>

1 个答案:

答案 0 :(得分:4)

尝试选择属于具有必需类的表单字段的所有输入,并将其required属性设置为true。

$('.form-field.required :input').prop('required', true);