简体   繁体   中英

jQuery Select Item by ID issue in Rails 3.1

I am trying to select some select box items by their ID in jQuery (in rails 3.1), with the following code:

$('#device_name').addClass("newSpecialClass");

but this (along with anything else I try to do to the item) is not working. No errors are generated in the console (though I have also checked using an ID that definitely does not exist, and I did not get any errors there either).

When I view the page, I can see the line of jQuery in the served assets (though I was expecting a single compiled application.js file, but it seems to be keeping them separate).

Can anyone suggest any reasons why this does not seem to be working? For reference, the html looks like:

<select id="device_name" name="device[name]">
  <option value="">--</option>
  <option value="Brass Floor Lamp" class="1">Brass Floor Lamp</option>
  <option value="Paper Floor Lamps" class="1">Paper Floor Lamps</option>
  <option value="Chandelier" class="1">Chandelier</option>
  <option value="Desk Lamps" class="4">Desk Lamps</option>
  <option value="Paper Floor Lamp" class="4">Paper Floor Lamp</option>
</select>

Any help appreciated.

Thanks!

EDIT: I have confirmed Query is loaded by doing:

if (jQuery) {
alert("jQuery Loaded!");
}

which results in a popup window displaying the message.

Is your code in a document ready event?

$(document).ready(function(){
    // Jquery here
});

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