开发者

Ruby on Rails: I need to invoke an observer_field when I programmatically change the value of a select, how can I do that?

开发者 https://www.devze.com 2023-02-08 08:29 出处:网络
This part works: <%= observe_field \"dbe_filter\", :url => {:action => :filter_action, :only_path => :false },

This part works:

<%= observe_field "dbe_filter", :url => {:action => :filter_action, :only_path => :false },
                  :frequency => 0.5,
                    :with => "'value='+encodeURIComponent(value)"%>

My javascript: this part works

<script type="text/javascript">
  $j("#dbe_filter").change(function(){
    $stored_filter = $j(this).val();
  });
  $j("#dbe_filter").val($stored_filter);
  // what I want is something right here that will invoke the above 
  //   observe_field helper. 
  // Is there a way with jQuery (i'm using noConflict) to invoke a .change
  //   action?

  // current, non-working solution (node that the alert pops up):
   alert($stored_filter + " " + $j("#dbe_filter").val());
    $j("#dbe_filter").trigger('change');
  // assuming a solution works, I should see a proper XHR request in the webkit inspector.

</script>

The details of what is happening: I have a different select drop down, that re-renders part of a page, and upon re-rendering, all the new javascript is ran (hence why my alert message test works), but I don't understand why th开发者_运维百科e .trigger doesn't. I need change triggered, so that a different part of my code is triggered to draw a graph.

I'm surprised that changing the value of the select with javascript doesn't trip the 'change' trigger

The .trigger works. Just needs to be placed after the observe_field helper. • This question is solved


You can use trigger.

$j("#dbe_filter").trigger('change');

I would suggest using this code to test if it works:


<select id='dbe_filter'>
  <option value='NEW'>NEW</option>
  <option value='UPDATED'>UPDATED</option>
</select>

<script type="text/javascript">
  $("#dbe_filter").change(function(){
    $(this).val("UPDATED");
  });

  $("#dbe_filter").trigger('change');

</script>

0

精彩评论

暂无评论...
验证码 换一张
取 消