html - Javascript: Compare values in a table -
I am currently learning JavaScript and completing some courses on Codecadi and Coleschool. I now know a little bit about how you write functions in javascript. If I am familiar with more statements, but one thing is that I can not hang on now and this is the reason I am hoping that people can help me with this and understand how it works. That's why I can learn from this. Use myself in the future. I have searched all day and tried to do a lot of things, but it just does not work.
I have a table that contains some values (including JSField), these values are not stable because they are in the examples but will change everyday. Actually what is this: This is a table that shows how many hours a person has to work and how many hours worked. These values should be compared to see what is the difference. If there is a difference then the checked row should have X.
In JSfiddle I have put some javascript which does not work. But this is the same idea about which I should implement it (I am quite positive that it is something in that direction, but still I am just starting now).
This thing which I still can not understand in Javascript is how to implement this code in my HTML page so that it can work. I do not know how to get a fixed price inside my comparative function from the table .. if it makes sense?
Anyway, this code is
should be represented on window onload = function check (a, b) {/ * a and b hours a and b, Has not been disclosed * / for (i = 1; i & lt; = id.length; i ++) {/ * id length is a value that I have declared. * / If (a! = B) {/ * Nothing on check * /} and {/ * Keep x on check *}}} Click on Bella for the whole Code. Those values which are kept in constant, but they are going to be dynamic. That's why I need a function that goes through these numbers every time I load the page.
It does this:
function check () {var table = document.getElementById ("mytable"); Var difference; For (var i = 1; il; table.rows.length; i ++) {difference = (table.ros [i]. Seals [2] .winnerHTML * 1) - (table.ro [i]. Seals [3]. WinnerHTML * 1); Table. Row [i] Seals [4] .innerHTML = Difference; Table. Fury [i]. Seals [5] .innerHTML = difference == 0? '': 'X'; }} Window.onload = check; Notes:
- Inline
onload was deleted because it is set to javascript - moved
checks () puts on a punching function and it is assigned to window.onload , when the page loads, call it < li> I gave the table an ID only, the simplicity of handling it for this demo, but you can use any type of table you want to get ( The class name for Dahrn). - This table has been omitted from the table by "> 1 .
- For the
* 1 subtraction shown in the code The string of the cell is casting to the content number. / Li>
jQuery version for completion:
$ (Function () {var table = $ ('# mytable'), difference, table.fund ('toddy & gt; tr') each (function () {difference = ($ (this) .fund ('td: Eq (2) '). Text () * 1) - ($ (This) .find (' td: eq (3) '). Text () * 1); $ (this) .find (' td: eq (4) '). Text (difference); $ (this) .find (' td: Eq (5) '). Text (difference == 0?' ':' X ');});}); Note: if you have an image with X Use .text () instead of .html () , or attach the image element.
Comments
Post a Comment