如果未选中复选框,则显示警告消息(Display alert message if checkbox isn't checked)

编程入门 行业动态 更新时间:2024-10-25 23:28:15
如果未选中复选框,则显示警告消息(Display alert message if checkbox isn't checked)

如果未检查此页面上的复选标记,我试图显示一条消息 - https://www.hawaiidiscount.com/rental-cars/oahu/honolulu-airport.htm

看看它的位置“我知道Alamo有额外费用,在拿车时直接向柜台支付给Alamo。”

在静态页面上,下面的脚本可以正常工作(例如,如果您下载页面并将代码放在正文中)。 但是当页面生效时,它无法正常工作。

<script> $('#dnn_ctr367_CartQuickView_anchBookNow').click(function () { if (!$('.cartcheckbox').is(':checked')) { alert('Please check the box - I\'m aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle.'); return false; } }); </script>

知道什么可能是错的吗?

编辑:当您单击现在的书籍按钮时,显示在购物车顶部的文本(如果您没有提供任何所需信息)。 因此,该文本似乎是忽略上述脚本的程序的一部分。 有没有办法让我的脚本在另一个之前激活,所以如果你点击绿色按钮,你会立即看到弹出消息?

I am trying to show a message if the checkmark on this page is not checked - https://www.hawaiidiscount.com/rental-cars/oahu/honolulu-airport.htm

See where it says "I'm aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle."

On a static page the script below works correctly (for example if you download the page and place the code in the body). But when the page is live, it is not working.

<script> $('#dnn_ctr367_CartQuickView_anchBookNow').click(function () { if (!$('.cartcheckbox').is(':checked')) { alert('Please check the box - I\'m aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle.'); return false; } }); </script>

Any idea what might be wrong?

EDIT: When you click on the book now button there is text that appears on top of the cart (that's in case you didn't put any of the required information). So it seems this text is part of a program that ignores the script above. Is there a way to make my script to activate before the other one so if you click the green button you will right away see the pop up message?

最满意答案

这是因为$('.cartcheckbox')不是一个复选框,它是一个包含复选框的范围

尝试将其更改为复选框的ID(如果页面中只有一个复选框,则input[type="checkbox"] 。

如:

$('#dnn_ctr367_CartQuickView_anchBookNow').on('click', function () { // update selector below if (!$('input[type="checkbox"]').is(':checked')) { alert('Please check the box - I\'m aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle.'); return false; } });

如果页面上有多个复选框,请尝试将其更改为

if (!$('.cartcheckbox').find('input[type="checkbox"]').is(':checked')) {...}

仅选择相关span内的复选框

更新下面的代码以包含触发按钮

$(function() {

  $('#dnn_ctr367_CartQuickView_anchBookNow').on('click', function() {
    // updated selector below    
    if (!$('.cartcheckbox').find('input[type="checkbox"]').is(':checked')) {

      alert('Please check the box - I\'m aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle.');

      return false;

    } else {
      alert('checkbox is checked');
    }
  });
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="cartcheckbox"><input id="dnn_ctr367_CartQuickView_ucCollector_I'mawareAlamohasadditionalfees,paiddirectlytoAlamoatthecounterwhenpickingupthevehicle." type="checkbox" name="dnn:ctr367:CartQuickView:ucCollector:I'mawareAlamohasadditionalfees,paiddirectlytoAlamoatthecounterwhenpickingupthevehicle."></span>
<br/>
<div align="center">
  <a id="dnn_ctr367_CartQuickView_anchBookNow" style="TEXT-DECORATION: none" href="#">
    <button>
      <div class="TBook">Book Now</div>
    </button>
  </a>
</div> 
  
 

It's because $('.cartcheckbox') is not a checkbox, it's a span containing the checkbox

Try changing it to the id of the checkbox (or input[type="checkbox"] if you only have one checkbox in the page.

As in:

$('#dnn_ctr367_CartQuickView_anchBookNow').on('click', function () { // update selector below if (!$('input[type="checkbox"]').is(':checked')) { alert('Please check the box - I\'m aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle.'); return false; } });

If you have more than one checkbox on the page, try changing it to

if (!$('.cartcheckbox').find('input[type="checkbox"]').is(':checked')) {...}

to select only the checkbox inside your relevant span

Updated code below to include trigger button

$(function() {

  $('#dnn_ctr367_CartQuickView_anchBookNow').on('click', function() {
    // updated selector below    
    if (!$('.cartcheckbox').find('input[type="checkbox"]').is(':checked')) {

      alert('Please check the box - I\'m aware Alamo has additional fees, paid directly to Alamo at the counter when picking up the vehicle.');

      return false;

    } else {
      alert('checkbox is checked');
    }
  });
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="cartcheckbox"><input id="dnn_ctr367_CartQuickView_ucCollector_I'mawareAlamohasadditionalfees,paiddirectlytoAlamoatthecounterwhenpickingupthevehicle." type="checkbox" name="dnn:ctr367:CartQuickView:ucCollector:I'mawareAlamohasadditionalfees,paiddirectlytoAlamoatthecounterwhenpickingupthevehicle."></span>
<br/>
<div align="center">
  <a id="dnn_ctr367_CartQuickView_anchBookNow" style="TEXT-DECORATION: none" href="#">
    <button>
      <div class="TBook">Book Now</div>
    </button>
  </a>
</div> 
  
 

更多推荐

本文发布于:2023-04-27 12:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   消息   Display   alert   checkbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!