Jquery Css无法高亮输入类型按钮标签文本onclick单选按钮

标签 jquery html css forms

我想知道如何在单击单选按钮时标记输入类型单选按钮标签文本。在 SO 中的一些答案的帮助下,它可以在标签点击上工作,但是当我点击单选按钮时我无法标记突出显示。

$('label').click(function() {
  $('label').removeClass('orangeBackground');
  $(this).addClass('orangeBackground');
});
.orangeBackground {
  color: #CE5A0B;
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal fade listing-id" id="closeListingModal" role="dialog">
  <div class="modal-dialog closelistingmodal-dialog">
    <!-- Modal content-->
    <div class="modal-content closelistingmodal-content">
      <form class="closelisting-form form-horizontal" onsubmit="return false" role="form" data-listing-id="" action="">
        <input type="hidden" name="listing_id">
        <input type="hidden" name="listing_weight">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title"></h4>
        </div>
        <div class="closelistingmodal-body modal-top-padding">
          <div class="col-sm-12 ">
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <input type="radio" name="reason-closing" value="1" id="rad1"><label for="rad1"> Rate Mismatch</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <input type="radio" name="reason-closing" value="2" id="rad2"><label for="rad2"> Program Cancelled / Differed</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <input type="radio" name="reason-closing" value="3" id="rad3"><label for="rad3"> Vehicle Unavailable</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <input type="radio" name="reason-closing" value="4" id="rad4"><label for="rad4"> Listing Fulfilled By Party</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <input type="radio" name="reason-closing" value="5" id="rad5"><label for="rad5"> Payment Terms Mismatch</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <input type="radio" name="reason-closing" value="6" id="rad6"><label for="rad6"> Wrong Post</label>
            </div>
          </div>
          <div class="form-group col-sm-12">
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 ">
              <label>Rate PMT:</label>
              <input class=" form-control row-margin input-pmtftl-amount" type="text" id="closelistinginput-value" name="listing_pmt" value="" placeholder="Rate In PMT (₹)" / min="0">
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 no-of-trucks">
              <label>Weight (MT):</label>
              <input class="no-of-trucks form-control " type="text" name="listing_weight_mt" value="" id="noOfTrucks" min="1" />
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 ">
              <label>Rate FTL:</label>
              <input class=" form-control input-pmtftl-amount" type="text" id="closelistingftl-input" name="listing_ftl" value="" placeholder="Rate In FTL (₹)" / min="0">
            </div>
          </div>
          <div class="form-group col-sm-12">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hidden-lg hidden-md hidden-sm">
              <textarea type="text" class=" form-control row-margin" name="listing_notes" placeholder="Notes" /></textarea>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hidden-xs">
              <textarea type="text" class=" form-control row-margin" name="listing_notes" placeholder="Notes" /></textarea>
            </div>
          </div>
        </div>
        <div class="modal-footer ">
          <button class="v2-button  closing-submit-btn" name="closelisting-submit">Submit</button>
        </div>
      </form>
    </div>
  </div>
</div>

图片 1 .. First snippet when i clicked its respective label

图 2... When i clicked radio button previous label won't unmarked with new one

任何帮助将不胜感激。 谢谢

最佳答案

您仅将点击应用到标签,输入复选框在其外部并且不会触发点击,因此只需将其放在标签内,如下所示:

$('label').click(function() {
  $('label').removeClass('orangeBackground');
  $(this).addClass('orangeBackground');
});
.orangeBackground {
  color: #CE5A0B;
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal fade listing-id" id="closeListingModal" role="dialog">
  <div class="modal-dialog closelistingmodal-dialog">
    <!-- Modal content-->
    <div class="modal-content closelistingmodal-content">
      <form class="closelisting-form form-horizontal" onsubmit="return false" role="form" data-listing-id="" action="">
        <input type="hidden" name="listing_id">
        <input type="hidden" name="listing_weight">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title"></h4>
        </div>
        <div class="closelistingmodal-body modal-top-padding">
          <div class="col-sm-12 ">
            <div class="col-md-12 col-xs-12 cell-no-padding">
             <label for="rad1"> <input type="radio" name="reason-closing" value="1" id="rad1"> Rate Mismatch</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
             <label for="rad2"> <input type="radio" name="reason-closing" value="2" id="rad2"> Program Cancelled / Differed</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <label for="rad3"> <input type="radio" name="reason-closing" value="3" id="rad3">Vehicle Unavailable</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <label for="rad4"><input type="radio" name="reason-closing" value="4" id="rad4"> Listing Fulfilled By Party</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <label for="rad5"> <input type="radio" name="reason-closing" value="5" id="rad5">Payment Terms Mismatch</label>
            </div>
            <div class="col-md-12 col-xs-12 cell-no-padding">
              <label for="rad6"><input type="radio" name="reason-closing" value="6" id="rad6"> Wrong Post</label>
            </div>
          </div>
          <div class="form-group col-sm-12">
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 ">
              <label>Rate PMT:</label>
              <input class=" form-control row-margin input-pmtftl-amount" type="text" id="closelistinginput-value" name="listing_pmt" value="" placeholder="Rate In PMT (₹)" / min="0">
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 no-of-trucks">
              <label>Weight (MT):</label>
              <input class="no-of-trucks form-control " type="text" name="listing_weight_mt" value="" id="noOfTrucks" min="1" />
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 ">
              <label>Rate FTL:</label>
              <input class=" form-control input-pmtftl-amount" type="text" id="closelistingftl-input" name="listing_ftl" value="" placeholder="Rate In FTL (₹)" / min="0">
            </div>
          </div>
          <div class="form-group col-sm-12">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hidden-lg hidden-md hidden-sm">
              <textarea type="text" class=" form-control row-margin" name="listing_notes" placeholder="Notes" /></textarea>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hidden-xs">
              <textarea type="text" class=" form-control row-margin" name="listing_notes" placeholder="Notes" /></textarea>
            </div>
          </div>
        </div>
        <div class="modal-footer ">
          <button class="v2-button  closing-submit-btn" name="closelisting-submit">Submit</button>
        </div>
      </form>
    </div>
  </div>
</div>

关于Jquery Css无法高亮输入类型按钮标签文本onclick单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47188506/

相关文章:

javascript - 无法在 javascript 中获取 div 上的单选按钮值

html - div中的图片因高差被截断,如何完整显示?

html - 如何自定义 bootstrap 默认顶部固定导航,如下图所示?

javascript - 保持建议框始终直接在文本框下方

jquery - 结合 jquery 图像切换和滚动字幕脚本

javascript - 使用 jquery 将 html 元素从 div 更改为 ul

javascript - 如何在 jQuery Datepicker ui 中阻止多个日期范围

jQuery 与用户一起滚动 div 直到到达包含 div 的底部

html - 如何水平居中元素?

javascript - onclick打开关闭div报错