简体   繁体   English

jQuery / Ajax选择框onChange触发器

[英]Jquery/Ajax selectbox onChange trigger

Ok, what i'm trying to do is to create a list of select boxes which update their value each depending on the above: 好的,我想做的是创建一个选择框列表,根据上面的内容更新每个框的值:

the first one is like that: 第一个是这样的:

<script>

var $schoolRegion = $('#stage_attendedSchoolRegion');
$schoolRegion.change(function() {
  var $form = $(this).closest('form');
  var data = {};
  data[$schoolRegion.attr('name')] = $schoolRegion.val();
  $.ajax({
    url : $form.attr('action'),
    type: $form.attr('method'),
    data : data,
    success: function(html) {
      $('#stage_attendedSchoolDistrict').replaceWith(
        $(html).find('#stage_attendedSchoolDistrict')
      );
    }
  });
});

</script>

when it's value changes an ajax request is dispatched, and the choices of the "#stage_attendedSchoolDistrict" select are updated with the POST html value. 当其值更改时,将调度ajax请求,并使用POST html值更新“ #stage_attendedSchoolDistrict”选择的选项。

Since here's all good, now i need to do the same with the "#stage_attendedSchoolDistrict" select so: 既然一切都很好,那么我现在需要对“ #stage_attendedSchoolDistrict”进行同样的选择:

<script>

var $schoolDistrict = $('#stage_attendedSchoolDistrict');
$schoolDistrict.on.change(function() {
  var $form = $(this).closest('form');
  var data = {};
  data[$schoolDistrict.attr('name')] = $schoolDistrict.val();
  $.ajax({
    url : $form.attr('action'),
    type: $form.attr('method'),
    data : data,
    success: function(html) {
      $('#stage_attendedSchoolCity').replaceWith(
        $(html).find('#stage_attendedSchoolCity')
      );
    }
  });
});

</script>

The problem is that the event doesn't trigger at all. 问题在于该事件根本不会触发。 I'm thinking it is related to the fact that the former has values in the HTML of the page while the latter doesnt. 我认为这与以下事实有关:前者在页面的HTML中具有值,而后者没有。 How can i solve? 我该如何解决?

Any suggestion is appreciated, thanks for the help. 任何建议表示赞赏,感谢您的帮助。

您在第二个示例中调用的是.change,而不是第一个示例中的.change。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM