获取最接近输入的值Jquery

时间:2017-02-05 14:22:46

标签: javascript jquery html

当我点击id save_newsection时,我需要class school_name的值意味着学校名称

<div class="school_form_submit">
  <div class="form-group">
    <label class="control-label col-md-3">School Name<span class="required"> * </span>
    </label>
    <div class="col-md-9">
      <input type="text" class="form-control school_name" placeholder="Please Enter School Name" value="dfghdf" name="school_name_submit">
    </div>
  </div>
</div>
<div class="modal-footer">
  <button type="button" class="btn dark btn-outline" data-dismiss="modal">Close</button>
  <button type="button" id="save_newsection" class="btn green">Save changes</button>
</div>
</div>

1 个答案:

答案 0 :(得分:1)

如果只有一个类.school_name的输入,请使用此

$("#save_newsection").click(function() {
    var value = $("input.school_name").val();
    // use value
});

如果整个文档中可能有更多这样的内容,请使用:

$("#save_newsection").click(function() {
    var value = $(".school_form_submit .school_name").val();
    // use value
});

$("#save_newsection").click(function() {
    var value = $(".school_form_submit .school_name").val();
    alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="school_form_submit">
  <div class="form-group">
    <label class="control-label col-md-3">School Name
      <span class="required"> * </span>
    </label>
    <div class="col-md-9">
      <input type="text" class="form-control school_name" placeholder="Please Enter School Name" value="dfghdf" name="school_name_submit">
    </div>
  </div>
</div>
<div class="modal-footer">
  <button type="button" class="btn dark btn-outline" data-dismiss="modal">Close</button>
  <button type="button" id="save_newsection" class="btn green">Save changes</button>
</div>