【问题标题】:Jquery Css unable to highlight input type button label text onclick radio buttonJquery Css无法突出显示输入类型按钮标签文本onclick单选按钮
【发布时间】:2017-11-08 19:52:48
【问题描述】:

我想知道如何在单击单选按钮上标记输入类型单选按钮标签文本。在 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 ..

图 2...

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

【问题讨论】:

  • 已转换为 sn-p,我没有发现任何问题。
  • @SergChernata 检查这个 sn-p 。 jsfiddle.net/pw8Ld6cs 如果您点击任何 abel,它会以突出显示的颜色进行检查,但如果您点击任何单选按钮,其各自的标签将不会改变。
  • 标签什么时候变回黑色?
  • @ErikPhilips 当我检查一个新按钮时。
  • @TanmoySarkar 是同名的单选按钮吗?

标签: jquery html css forms


【解决方案1】:

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

$('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>

【讨论】:

    【解决方案2】:

    试试这个解决方案

    input[type="radio"]:checked+label{
        color: #CE5A0B;
        font-weight: bold;
      } 
    

    https://jsfiddle.net/cxerg55f/

    【讨论】:

      【解决方案3】:

      应该这样做,使用一个类(这里我称之为“radioBtns”)作为将捕获点击的输入/标签。

      $(".radioBtns").click(function() {
        $('label').removeClass('orangeBackground');
        $(this).parent().children("label").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">
                  <div class="col-md-12 col-xs-12 cell-no-padding">
                    <input type="radio" name="reason-closing" value="1" id="rad1" class='radioBtns'><label for="rad1" class='radioBtns'> 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" class='radioBtns'><label for="rad2" class='radioBtns'> 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" class='radioBtns'><label for="rad3" class='radioBtns'> 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" class='radioBtns'><label for="rad4" class='radioBtns'> 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" class='radioBtns'><label for="rad5" class='radioBtns'> 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" class='radioBtns'><label for="rad6" class='radioBtns'> 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>

      【讨论】:

      • 在将其应用于块元素时请注意,因此当我单击整行时会触发单击。尝试点击空白处,你会看到。
      • 好收获!更新以修复错误。将类直接应用于每个无线电输入和标签。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      相关资源
      最近更新 更多