简体   繁体   中英

Unresponsive javascript when using collection_select?

I have just started learning rails, html and javascript. I am using a collection_select to allow a user to select other users in the database:

<%= f.collection_select :id, Customer.where(business_id: current_customer.business_id), :id, :full_name, :prompt => 'Select', :html => { :id => "colleageselect", :onChange => "renderColCal(this)"} %>

<div id = colleaguecal> </div>

I have just been trying to test whether onChange works, by using the javascript:

<script type = "text/javascript">
    function renderColCal(select){
        alert('this is a test' + select.valueOf() );
        document.getElementById("colleaguecal").innerHTML = "foo"

    }
</script>

But changing the collection_select value when running the page doesn't do anything? Am I missing something here?

check the generated html if it is what you are expecting. I don't see any issues with the js although it would be better if it was unobstrusive. The only thing that may have caused your issue is that you didn't just passed a hash to the html options. Try

f.collection_select :id,
  Customer.where(business_id: current_customer.business_id),
  :id,
  :full_name,
  { prompt: 'Select' },
  { id: "colleageselect", onChange: "renderColCal(this)" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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