unobtrusive-javascript

jQuery how to trigger event after form post returns?

心已入冬 提交于 2019-12-06 03:48:07
I have an expensive form action that builds a zip file on the server and returns it to the browser. <form action='/download' method='post'> <input type='submit' value='download'/> </form> I want to block the page on click of the button so that the user doesn't repeatably hit the button. However I want to unblock the page after the form returns. How can trigger an event on successful completion of the form? (I know I can trigger this by changing the form to be an ajax submission but then the save file dialog does not appear...) Any suggestions? Thanks One way you could handle this without using

Which is faster, .on() or .click()?

狂风中的少年 提交于 2019-12-06 03:47:00
If I have a table with links to delete a record, which is the best way to wire up the click event? Do these evaluate the same under the hood? $("#table").on("click", ".delete", function(){ //do stuff }); or $("#table .delete").click(function(){ //do stuff }); No, they don't. In the way you are using them... .on() The way you are using it (delegation) , will save you cycles at attachment as well as memory, because it doesn't actually attach the events in that moment, as it provides a dynamic element where the event is applied to future elements as well. .click() will save you cycles at

jQuery Accordion definition list with multiple description items

那年仲夏 提交于 2019-12-05 11:14:44
I can't seem to use jQuery Accordions with definition lists that have multiple desciption items (dd). The author's examples have only single dd items. In the example below B2, B3 & C2 show onLoad, rather than hide like A1, B1 & C1 as I would prefer. How would I achieve this? jQuery('dl').accordion({ event: 'click', active: false, animated: "bounceslide", header: "dt" });​ <dl> <dt>A</dt> <dd>A1</dd> <dt>B</dt> <dd>B1</dd> <dd>B2</dd> <dd>B3</dd> <dt>C</dt> <dd>C1</dd> <dd>C2</dd> </dl> ( Live jsFiddle version ) demo: http://so.lucafilosofi.com/jquery-accordion-definition-list-with-multiple

Disable link with the prototype observe method

丶灬走出姿态 提交于 2019-12-05 02:54:33
I want to create a link like this: <a href="http://example.com">text</a> and replace the behavior so that the link downloads the content with Ajax instead when clicking. It is important for me not to replace the href attribute (so copying the link still works). One solution would be to do: $('link').onclick = function() { return false; }; but I would like to use the .observe method. But this doesn't work: $('link').observe('click', function() { return false; }); (which is quite logical). Any ideas on how I could achieve this? Thanks. $('link').observe('click', function(e) { Event.stop(e); });

MVC3 DataAnnotationsExtensions error using numeric attribute

a 夏天 提交于 2019-12-05 01:34:10
问题 I've installed Scott's Kirkland DataAnnotationsExtensions. In my model I have: [Numeric] public double expectedcost { get; set; } And in my View: @Html.EditorFor(model => model.expectedcost) Now, when the page tries to render I get the following error: Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: number Any ideas why I'm getting the error ? 回答1: The quick answer is simply remove the attribute [Numeric] The

Rails 3: How do I call a javascript function from a js.erb file

我怕爱的太早我们不能终老 提交于 2019-12-05 00:57:01
问题 Now that I've upgraded to Rails 3, I'm trying to figure out the proper way to separate and reuse pieces of javascript. Here's the scenario I'm dealing with: I have a page with two areas: one with elements that should be draggable, the other with droppables. When the page loads I use jQuery to setup the draggables and droppables. Currently I have the script in the head portion of application.html.erb, which I'm sure is not the right solution but at least works. When I press a button on the

Do you plan for javascript being off?

感情迁移 提交于 2019-12-04 22:16:31
问题 I'm coding a fairly large and complex site by myself, so do you think I need to support javascript being turned off? Its a lot of extra work supporting full page postbacks for stuff I could quickly do with JSON and ajax. 回答1: You need to write server-side code in any case to handle the posts, whether they are from AJAX or not. So why not code according to the DRY principle, and reuse the same logic for standard postbacks and AJAX requests? 回答2: I think you should support that. In fact, if

Implementing Unobtrusive Javascript

泪湿孤枕 提交于 2019-12-04 18:46:35
I'm reworking an old website and am focusing on making the Javascript/jQuery as unobtrusive as possible. I'm looking for some advice on one part of the project: UJ requires that, if Javascript is turned off in a browser, all the site content is still available to the user. One part of my site has a large list of items. Each item in the list has it's own description, which uses CSS display:none to hide it by default. Clicking on the item toggles the visibility of the description using jQuery .toggle():, so a person unable to use Javascript (for whatever reason) would never see it. If I use

Best practices for using AJAX without Rails' built-in helpers?

社会主义新天地 提交于 2019-12-04 16:38:46
In my blog application, some posts appear as excerpts -- i.e., the user sees the first (say) 500 characters, and can click a link to view the entire post. Here is the relevant partial: <% href = url_for post_path(:id => post) %> <h1 class="title"><%= post.title %></h1> <h2 class="published_on"><%= post.author %> wrote this <%= time_ago_in_words(post.published_on)%> ago</h2> <div class="body"> <% if defined?(length) %> <%= truncate_html(post.body, :length => length, :omission => "…<h1><a class='more' href=\"#{href}\">Click here for more!</a></h1>") %> <% else %> <%= post.body %> <% end %> </div

How to handle JavaScript events (e.g. 'link_to :remote') the Rails Way?

不问归期 提交于 2019-12-04 13:11:34
I am using Ruby on Rails 4 and I would like to handle JavaScript events the Rails way. That is, for example, given I have the following link_to('destroy', article_path(@article), :method => :delete, :remote => true) when I click on the above generated link then I would like the success JS event to update the page content by removing the deleted article DOM element. The AJAX response, whatever it is, should be ignored. I am aware of the existence of the rails.js file but I don't how to use it in order to accomplish what I am looking for. There is a wiki page https://github.com/rails/jquery-ujs