如果由 JQuery 添加,则选择不会触发的更改

发布于 2024-12-21 19:12:39 字数 826 浏览 6 评论 0原文

我有一个多级选择链,其中第一个选择的值生成下一个选择列表的选项。在第二个列表中,某些值将导致 div 与另一个输入一起显示。

在静态内容上测试时,我的代码(如下)似乎工作得很好(即:第二个选择是硬编码在 html 中)。但是当我用 JQuery 添加它时,第二级不再触发 .change 函数。

<script type="text/javascript">
$(document).ready(function() {
  $dts = $("select[name='tourdes']");
  $dts.change(function() { 
     var dtsValue = $(this).val();
     var dtsString = '?tourdes=' + dtsValue; 
     $('#dateSelect').show();
     $('#dateSelect').load('include/avdates.php' + dtsString).append();
  });
});
</script>

<script type="text/javascript">
$(document).ready(function() {
  $tags = $("select[name='tourcode']");
  $tags.change(function() {
     if ($(this).val() == "private") {
     $(".prvcal").css({"visibility":"visible"});
  }
  });
});
</script>

我猜测有些东西需要重新初始化,但我的实验没有得到任何结果。

I have a multi level select chain, where by the value of the first select generates the options for the next select list. And within the second list some of the values will cause a div to display with another input.

My codes (below) seem to work just fine when tested on static content (ie: the second select is hard coded in the html). But when I add it with JQuery, the second level no longer triggers the .change function.

<script type="text/javascript">
$(document).ready(function() {
  $dts = $("select[name='tourdes']");
  $dts.change(function() { 
     var dtsValue = $(this).val();
     var dtsString = '?tourdes=' + dtsValue; 
     $('#dateSelect').show();
     $('#dateSelect').load('include/avdates.php' + dtsString).append();
  });
});
</script>

<script type="text/javascript">
$(document).ready(function() {
  $tags = $("select[name='tourcode']");
  $tags.change(function() {
     if ($(this).val() == "private") {
     $(".prvcal").css({"visibility":"visible"});
  }
  });
});
</script>

I am guessing something needs to be re-initialized, but I am getting no where with my experiments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌辣 2024-12-28 19:12:39

如果您使用的是 jQuery 1.7,则需要使用 on,因为 livedelegate 均已弃用。

$(document).on("change", "select[name='tourcode']", function() {
    var dtsValue = $(this).val();
    var dtsString = '?tourdes=' + dtsValue; 
    $('#dateSelect').show();
    $('#dateSelect').load('include/avdates.php' + dtsString).append();
});

on() 文档

If you're using jQuery 1.7 you'll want to use on, as both live and delegate are deprecated.

$(document).on("change", "select[name='tourcode']", function() {
    var dtsValue = $(this).val();
    var dtsString = '?tourdes=' + dtsValue; 
    $('#dateSelect').show();
    $('#dateSelect').load('include/avdates.php' + dtsString).append();
});

docs for on()

つ低調成傷 2024-12-28 19:12:39

您可能正在使用动态生成的 HTML 元素。如果是这种情况,您需要使用 .delegate() 来处理它们:

$('select').delegate("[name='tourdes']", 'change', function() { 

You are probably using dynamically-generated HTML elements. If that is the case, you need to use .delegate() to handle them:

$('select').delegate("[name='tourdes']", 'change', function() { 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文