简体   繁体   English

单击复选框后,我的复选框更改事件不起作用。 我正在使用bootstrap和jquery

[英]My checkbox change event is not working when clicked. I am using bootstrap and jquery

I want a function to be called everytime the checkboxes are clicked. 我希望每次单击复选框时都调用一个函数。 But, the checkbox.click or the checkbox.change does not seem to trigger any event. 但是,checkbox.click或checkbox.change似乎不会触发任何事件。 I have also tried $(document).on("click",.....) etc., but nothing works. 我也尝试了$(document).on(“ click”,.....)等,但是没有任何效果。

This is my HTML code. 这是我的HTML代码。

<div class="row">
              <div class="col-sm-6 deliveryCheckbox">
                <h4>Address for Delivery:</h4>
              </div>

            </div>
            <div class="row">

              <div class="col-sm-12">
                <div class="form-element deliveryCheckbox">
                  <label class="form-control deliveryCheckbox" style="border: none;"><input type="checkbox" id="centreDelivery" name="centreDelivery" class="form-control" checked="checked">Collect from Centre</label>

                </div>
              </div>

            </div>
            <div class="row deliveryAddress">

              <div class="col-sm-12">
                <div class="form-element deliveryCheckbox">
                    <label class="form-control deliveryCheckbox" style="border: none;"><input type="checkbox" id="deliveryAddressSame" name="sameDeliveryAddress" class="form-control" checked="checked">Same as above Address</label>

                </div>
              </div>

            </div>
            <div class="row deliveryAddress">
              <div class="form-element">
                <label for="deliveryAddress">Delivery Address:</label>
                <textarea class="form-control" rows="5" id="deliveryAddress" placeholder="Enter Delivery Address"></textarea>
              </div>
            </div>

And this is my jquery code: 这是我的jQuery代码:

// Managing the delivery address.


function checkDeliveryAddress(){

    if($('#centreDelivery').is(':checked')){
      $(".deliveryAddress").css('display','none');
    } else {
      $(".deliveryAddress").css('display','block');
    }
    if($('#deliveryAddressSame').is(':checked')){
      $("#deliveryAddress").text(
        $("#first_name").val() + " " + $("#last_name").val() + ", " +
        $("#address").val() + ", " + $("#city").val() + ": " + 
        $("#zip").val() + ", " + $("#state").find('option:selected').text() + ", " +
        $("#country").find('option:selected').text() + "."
      )
      $("#deliveryAddress").attr("disabled", "disabled");
    } else {
      $("#deliveryAddress").removeAttr("disabled");
      $("#deliveryAddress").text("");
      $("#deliveryAddress").focus();
    }
  }
  checkDeliveryAddress()
  $(document).on("click", ".deliveryCheckbox", function () {
    alert("Hey, something clicked."); 
    checkDeliveryAddress();

  })

  $(document).on("change", ":checkbox", function () {
    alert("Hey, something clicked."); 
    checkDeliveryAddress();

  })

  $(document).on("change", "#deliveryAddressSame", function () {
    alert("Hey, something clicked."); 
    checkDeliveryAddress();

  })

I have tried all the alternatives to get my code working, but it is not. 我已经尝试了所有替代方法来使代码正常工作,但事实并非如此。 The code works when I click on the first row. 当我单击第一行时,代码起作用。 But, there is no response when I click on the checkbox or their label. 但是,当我单击复选框或标签时,没有任何反应。

Please let me know if this helps: 请让我知道这可不可以帮你:

Here is the JS code. 这是JS代码。 I removed the lone call of checkDeliveryAddress(), and the .deliveryCheckbox class from some of your container divs, instead leaving that class only for the labels where you want the checkboxes. 我从您的某些容器div中删除了对checkDeliveryAddress()和.deliveryCheckbox类的单独调用,而不是仅将类保留在需要复选框的标签上。 Clicking on the checkboxes calls the checkDeliveryAddress function. 单击复选框将调用checkDeliveryAddress函数。 Here is a codepen : 这是一个codepen

// Managing the delivery address.


function checkDeliveryAddress(){

    if($('#centreDelivery').is(':checked')){
      $(".deliveryAddress").css('display','none');
    } else {
      $(".deliveryAddress").css('display','block');
    }
    if($('#deliveryAddressSame').is(':checked')){
      $("#deliveryAddress").text(
        $("#first_name").val() + " " + $("#last_name").val() + ", " +
        $("#address").val() + ", " + $("#city").val() + ": " + 
        $("#zip").val() + ", " + $("#state").find('option:selected').text() + ", " +
        $("#country").find('option:selected').text() + "."
      )
      $("#deliveryAddress").attr("disabled", "disabled");
    } else {
      $("#deliveryAddress").removeAttr("disabled");
      $("#deliveryAddress").text("");
      $("#deliveryAddress").focus();
    }
  }

  $(document).on("click", ".deliveryCheckbox", function () {
    alert("Hey, something clicked."); 
    checkDeliveryAddress();

  })

  $(document).on("change", ":checkbox", function () {
    alert("Hey, something clicked."); 
    checkDeliveryAddress();

  })

  $(document).on("change", "#deliveryAddressSame", function () {
    alert("Hey, something clicked."); 
    checkDeliveryAddress();

  })

Here is the HTML: 这是HTML:

<div class="row">
              <div class="col-sm-6 ">
                <h4>Address for Delivery:</h4>
              </div>

            </div>
            <div class="row">

              <div class="col-sm-12">
                <div class="form-element ">
                  <label class="form-control deliveryCheckbox" style="border: none;"><input type="checkbox" id="centreDelivery" name="centreDelivery" class="form-control" checked="checked">Collect from Centre</label>

                </div>
              </div>

            </div>
            <div class="row deliveryAddress">

              <div class="col-sm-12">
                <div class="form-element ">
                    <label class="form-control deliveryCheckbox" style="border: none;"><input type="checkbox" id="deliveryAddressSame" name="sameDeliveryAddress" class="form-control" checked="checked">Same as above Address</label>

                </div>
              </div>

            </div>
            <div class="row deliveryAddress">
              <div class="form-element">
                <label for="deliveryAddress">Delivery Address:</label>
                <textarea class="form-control" rows="5" id="deliveryAddress" placeholder="Enter Delivery Address"></textarea>
              </div>
            </div>

Try removing unnecessary code from the function and terminate lines with ";" 尝试从函数中删除不必要的代码,并以“;”终止行

function checkDeliveryAddress(){

    if($('#centreDelivery').is(':checked')){
      $(".deliveryAddress").css('display','none');
    } else {
      $(".deliveryAddress").css('display','block');
    }
    if($('#deliveryAddressSame').is(':checked')){
      $("#deliveryAddress").text(
        $("#first_name").val() + " " + $("#last_name").val() + ", " +
        $("#address").val() + ", " + $("#city").val() + ": " + 
        $("#zip").val() + ", " + $("#state").find('option:selected').text() + ", " +
        $("#country").find('option:selected').text() + "."
      );
      $("#deliveryAddress").attr("disabled", "disabled");
    } else {
      $("#deliveryAddress").removeAttr("disabled");
      $("#deliveryAddress").text("");
      $("#deliveryAddress").focus();
    }
}
checkDeliveryAddress();

$(document).on("change", ":checkbox", function () {
  alert("Hey, something clicked."); 
  checkDeliveryAddress();
});

Make sure that you have included jquery in reference. 确保已在参考中包含jquery。 Here's a working code. 这是一个工作代码。 plnkr.co/edit/JLsfRfTOjTIuPuL1aeuH?p=preview plnkr.co/edit/JLsfRfTOjTIuPuL1aeuH?p=preview

Ok, so you need to use $(document).on("click", ":checkbox", function() { for all your checkboxes. 好的,因此您需要对所有复选框使用$(document).on("click", ":checkbox", function() {

Secondly, I have removed / optimized / compressed the code in your function. 其次,我已经删除/优化/压缩了您函数中的代码。

You should chain them wherever possible, instead of getting the selector and using it again. 您应该尽可能地链接它们,而不是获取选择器并再次使用它。 For example, removeAttr("disabled").text('').focus() 例如, removeAttr("disabled").text('').focus()

Below is the updated code: 下面是更新的代码:

 function checkDeliveryAddress() { $(".deliveryAddress").show(); if ($('#centreDelivery').is(':checked')) { $(".deliveryAddress").hide(); } $("#deliveryAddress").removeAttr("disabled").text('').focus(); if ($('#deliveryAddressSame').is(':checked')) { $("#deliveryAddress").text( $("#first_name").val() + " " + $("#last_name").val() + ", " + $("#address").val() + ", " + $("#city").val() + ": " + $("#zip").val() + ", " + $("#state").find('option:selected').text() + ", " + $("#country").find('option:selected').text() + "." ); $("#deliveryAddress").attr("disabled", "disabled"); } } checkDeliveryAddress() $(document).on("click", ":checkbox", function() { console.log("Hey, something clicked."); checkDeliveryAddress(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="col-sm-6 deliveryCheckbox"> <h4>Address for Delivery:</h4> </div> </div> <div class="row"> <div class="col-sm-12"> <div class="form-element deliveryCheckbox"> <label class="form-control deliveryCheckbox" style="border: none;"><input type="checkbox" id="centreDelivery" name="centreDelivery" class="form-control" checked="checked">Collect from Centre</label> </div> </div> </div> <div class="row deliveryAddress"> <div class="col-sm-12"> <div class="form-element deliveryCheckbox"> <label class="form-control deliveryCheckbox" style="border: none;"><input type="checkbox" id="deliveryAddressSame" name="sameDeliveryAddress" class="form-control" checked="checked">Same as above Address</label> </div> </div> </div> <div class="row deliveryAddress"> <div class="form-element"> <label for="deliveryAddress">Delivery Address:</label> <textarea class="form-control" rows="5" id="deliveryAddress" placeholder="Enter Delivery Address"></textarea> </div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击复选框时无法发出事件。 (socket.io) - Unable to emit event when a checkbox is clicked. (socket.io) 我正在尝试生成随机颜色(javascript),并将结果应用于单击时的按钮。 如何将JS与Jquery合并? - I am trying generate a random color(javascript) and apply the result to a button when it is clicked. How do I incorporate my JS with my Jquery? 单击时,我的单选按钮不会更改为“已检查”。 我正在尝试使用它来更改显示的内容 - My radio buttons don't change to Checked when clicked. I'm trying to use this to change the content displayed 我正在 angular 中开发一个简单的列表应用程序,当我单击时,它将显示有关特定项目的详细信息。 请检查我的代码 - I'm working on a simple list application in angular that will show details about a specific item when I clicked. Please check my code 单击时,我的div不会slideDown()。 - My div will not slideDown() when clicked. 单击后无法更改图像。 图像仅更改一次,但不会更改回 - I can't get the image to change when clicked. The image only changes one time, but it will not change back 单击复选框不会删除新任务。 - New tasks won't delete when checkbox clicked. jQuery 更改事件不适用于复选框 - jQuery change event is not working with checkbox 单击复选框时的jQuery事件处理 - Jquery event handling when a checkbox is clicked 我想在单击时隐藏图像。 当element.onclick不起作用时调用函数 - I want to hide the image when clicked. Calling function when element.onclick not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM