Whether any support has been created in jQuery for basic survival checks, primarily expected 'Returned Elements Number of things like 'number' / P>
For example, I might have a simple statement like this:
$ ("#btn signup"). Click (function () {validateForm (););
Now a lot of reasons why $ ("# btn signup")
can not return exactly 1 item:
- You Wrong typed
- Someone accidentally named it
- It is not present on the page
- There are two elements with fault with that ID
- You are using ASP.NET MVC and you accidentally clicked with
HtmlHelper.Button (...)
instead ofHtmlHelper.Submit ()
For HTML has generated. Thebutton (...)
does not create an ID for the button button.
Now my application in this example (and many instances) is not working only until one item is returned from the selector so I always want to know if the $ ("@ # Btn signup")
does not return exactly 1 item, so how can I do it ?! I'm fine if this is an exception or preferably a warning box - so if I'm not running in a debugger I can be told.
I see the syntax like this - which is similar to the functionality for the .NET extension method.
$ ("#btn signup"). AssertSingle (). Click (function () {validateForm ();); Or $ ("#btn signup"). AssertSize (1). Click (function () {validateForm (););
I am always okay to run this code individually and let me know if there is any problem. This is clearly not to run this extra code for all infinity. In this example my code breaks if #btn signup
is not present.
I'm sure this problem has been beaten and there are many solutions - but can someone talk to me for some of them?
I do not see anything built in jQuery and will wonder what is the best plugin. I have something else on the page that can keep me 'looking' and tell me if there is a problem I do not even protest any AJAX call for any error reporting service.
It does not appear that anything has been made in it but writing an extension is not very difficult:
$ .fn.assertSize = function (size) {if (this.length! = Size) {warning ("expected" + size + "elements, but" + this.length + " Receive "."); // whatever, maybe using console.log to be more humorous) Return to it;};
Use exactly as you like In question.
$ ("#btn signup"). AssertSize (1) .clic K (function () {return certified form (););
Note that even if the function fails in its present form successfully, whatever claim you are chained, Will also be executed. <00> To prevent further execution of the series, return return false;
to return it;
.
Comments
Post a Comment