使 onchange 事件也运行到页面加载

发布于 2024-08-22 16:26:25 字数 1806 浏览 0 评论 0原文

我在 jQuery 中设置了一些 onchange 事件,这些事件用于根据另一个选择下拉列表中选择的内容填充选择下拉框。

我还需要这些在页面加载时运行,以便根据页面加载时第一个选择框的位置返回结果(否则我最终会得到一个空的选择框)。我浏览了互联网,但找不到任何完全适合我想要做的事情

我的 jQuery 代码是

$(function(){
        
    //Get Contacts for Company
    
      $("select#ContactCompanyId").change(function(){
        var url = "contactList/" + $(this).val() + "";
        $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '';
          $.each(j, function(key, value){
        options += '<option value="' + key + '">' + value + '</option>';
          })
          $("select#QuoteContactId").html(options);
        })
      })
    
    //Get list of product Categories
    
      $("select#ProductCategory").live('change', function(){
        var url = "productList/" + $(this).val() + "";
        var id = $(this).attr('name');
        $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '';
        options += '<option value="0">None</option>';
          $.each(j, function(key, value){
        options += '<option value="' + key + '">' + value + '</option>';
          })
          $("select#QuoteItem" + id + "product_id").html(options);
        })
      })
    
    //Function to add product data into the form

      $(".Product").live('change', function(){
        var url = "productData/" + $(this).val() + "";
        var id = $(this).attr('title');
        $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
        $("#QuoteItem" + id + "name").val(j['Product']['name']);
        $("#QuoteItem" + id + "price").val(j['Product']['price']);
        $("#QuoteItem" + id + "description").val(j['Product']['description']);
        })
      })
    })

提前感谢您的帮助

I have some onchange events setup in jQuery which are used to populate a select dropdown boxbased on what is selected in another select dropdown.

I need these to also run on page load that so that results are returned based on what the first select box is at on page load (otherwise I end up with an empty select box). I've had a look around the internets but can't find anything that fits exactly what I want to do

My jQuery code is

$(function(){
        
    //Get Contacts for Company
    
      $("select#ContactCompanyId").change(function(){
        var url = "contactList/" + $(this).val() + "";
        $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '';
          $.each(j, function(key, value){
        options += '<option value="' + key + '">' + value + '</option>';
          })
          $("select#QuoteContactId").html(options);
        })
      })
    
    //Get list of product Categories
    
      $("select#ProductCategory").live('change', function(){
        var url = "productList/" + $(this).val() + "";
        var id = $(this).attr('name');
        $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '';
        options += '<option value="0">None</option>';
          $.each(j, function(key, value){
        options += '<option value="' + key + '">' + value + '</option>';
          })
          $("select#QuoteItem" + id + "product_id").html(options);
        })
      })
    
    //Function to add product data into the form

      $(".Product").live('change', function(){
        var url = "productData/" + $(this).val() + "";
        var id = $(this).attr('title');
        $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
        $("#QuoteItem" + id + "name").val(j['Product']['name']);
        $("#QuoteItem" + id + "price").val(j['Product']['price']);
        $("#QuoteItem" + id + "description").val(j['Product']['description']);
        })
      })
    })

Thanks in advance for your help

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

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

发布评论

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

评论(2

盗梦空间 2024-08-29 16:26:25

您可以在使用 .trigger() 绑定事件后触发该事件,如下所示:

$("select#ContactCompanyId").change(function(){
    var url = "contactList/" + $(this).val() + "";
    $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
      $.each(j, function(key, value){
    options += '<option value="' + key + '">' + value + '</option>';
      })
      $("select#QuoteContactId").html(options);
    });
  }).trigger('change');

You can just trigger the event just after binding it with .trigger() like this:

$("select#ContactCompanyId").change(function(){
    var url = "contactList/" + $(this).val() + "";
    $.getJSON(url,{id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
      $.each(j, function(key, value){
    options += '<option value="' + key + '">' + value + '</option>';
      })
      $("select#QuoteContactId").html(options);
    });
  }).trigger('change');
橘寄 2024-08-29 16:26:25
$(document).ready(function() {  
    $('#your_select_id').change();
});

这将触发您选择的更改事件及其当前值。它相当于用户选择当前值,并且它将触发附加到它的任何由 onchange 事件触发的事件。

$(document).ready(function() {  
    $('#your_select_id').change();
});

This fires the change event for your select, with its current value. Its the equivlent of a user selecting the current value, and it will fire any events attached to it that would fire from an onchange event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文