在获取所有控件的值时,在Jquery中删除<br/>标签和按钮文本

时间:2017-03-21 17:37:22

标签: javascript jquery html

我试图使用JQuery获取Form的所有HTML控件的价值。这是我的HTML代码:

<div class="container">
  <div class="row">
    <div class="col-lg-12">
      <div class="col-lg-12">
        <div class="pull-right">
          <input type="text" name="CreatedDate" placeholder="DD/MM/YYYY" class="form-control" />
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="form-group">
        <label class="col-md-2 col-xs-4">
                    Respected  Dr.
                </label>
        <div class="col-md-10 col-xs-8">
          <input type="text" name="txtDoctor" placeholder="Doctor Name" class="form-control" />
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="form-group">
        <label class="col-xs-2">
                    Referring
                </label>
        <div class="col-xs-4 col-md-2">
          <select name="ddlGender" class="form-control">
                        <option value="Mr.">Mr.</option>
                        <option value="Mrs.">Mrs.</option>
                        <option value="Miss">Miss</option>
                    </select>
        </div>
        <div class="col-xs-6 col-md-8">
          <input type="text" name="txtPatient" placeholder="Patient Name" class="form-control" />
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="form-group">
        <label class="col-xs-9 col-sm-8 col-md-6">
                    to you for the favour of your kind consultation of
                </label>
        <div class="col-xs-3 col-sm-4 col-md-6">
          <select name="ddlGender" class="form-control">
                        <option value="his">his</option>
                        <option value="her">her</option>
                    </select>
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="form-group">
        <div class="col-xs-3">
          <select name="ddlGender" class="form-control">
                        <option value="his">C/O</option>
                    </select>
        </div>
        <label class="col-xs-9">
                    <input type="text" class="form-control" />
                </label>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="form-group">
        <label class="col-xs-9 col-sm-8 col-md-6">
                    Please do the needful to evaluate the case and treat
                </label>
        <div class="col-xs-3 col-sm-4 col-md-6">
          <select name="ddlGender" class="form-control">
                        <option value="him">him</option>
                        <option value="her">her</option>
                    </select>
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="form-group">
        <label class="col-xs-9 col-sm-8 col-md-6">
                    accordingly.
                </label>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <div class="col-lg-12">
        <div class="pull-right text-center">
          <label>
                        Regards and Respect<br />
                        from<br />
                        Dr. Jayesh Hathi
                    </label>
        </div>
      </div>
    </div>
  </div>
  <br />
  <div class="row">
    <div class="col-lg-12">
      <div class="col-xs-4">
        <input type="button" value="Save" class="btn btn-block btn-primary" id="BtnSave" />
      </div>
      <div class="col-xs-4">
        <input type="button" value="Print" class="btn btn-block btn-primary" />
      </div>
      <div class="col-xs-4">
        <input type="button" value="Cancel" class="btn btn-block btn-default" />
      </div>
    </div>
  </div>
</div>

使用以下JQuery,我获得了所有控件的价值:

$("#BtnSave").click(function() {
  var values = [];
  $('input,select,label').each(function() {
    if ($(this).val() != "") {
      values.push($(this).val().trim().replace(/&lt;br&gt;/g, " "));
    } else {
      values.push($(this).text().trim().replace(/&lt;br&gt;/g, " "));
    }
  });
  console.log(values);
});

正如您在附图中看到的那样,我在数组元素中获得了标签。我想删除它。我已经尝试用空格替换br标签。

另外,我不想要输入类型按钮的文本。我该怎么办?

输出图片:http://prnt.sc/emr80l
CodePen:http://codepen.io/anon/pen/ZexLaX?editors=1111

1 个答案:

答案 0 :(得分:1)

$("#BtnSave").click(function() {
  var values = [];
  $('input,select,label').each(function() {
    if($(this).attr('type') == 'button')) return true;
    if ($(this).val() != "") {
      values.push($(this).val().trim().replace(/&lt;br&gt;/g, " "));
    } else {
      values.push(($(this).text().trim().replace(/&lt;br&gt;/g, " ")).replace(/\s+/g, " "));
    }
  });
  console.log(values);
});

.replace(/\s+/g, " ")用一个空格替换连续的空格。