English 中文(简体)
• 如何在酒类上作一字节的发言?
原标题:how to run an if-else statement on jquery?

I have the code below. Now I want to run an if-else statement on it, but it s not working. Any idea with this?

var num1 = $( #id1 ).val(); 
var num2 = $( #id2 ).val();

var newVal = (num1+2)*(num2/4);
var nextVal =(num1+4)*(num2/2);
var newArr = new Array();
newArr.push(newVal);
newArr.push(nextVal);

newArr.sort();

if(newArr.get(0)==newVal)
$("body").html("<p>works fine!</p>");
else $("body").html("<p>works awfully!</p>");
最佳回答

你们宣布“Var”和资本五。

Var newVal = (num1+2)*(num2/4);

此外,出入阵列的内容有[指数],而不是植被(指数)

页: 1

  var num1 = 50;   
  var num2 = 100;

  var newVal = parseInt((num1+2)*(num2/4));
  var nextVal =parseInt((num1+4)*(num2/2));
  var newArr = new Array();
  newArr.push(newVal);
  newArr.push(nextVal);

  newArr = newArr.sort(function(a,b){return a - b});

  if(newArr[0]==newVal)
    $("body").html("<p>works fine!</p>");
  else
    $("body").html("<p>works awfully!</p>");​ 
问题回答

Just use newArr[0] und newArr.get(0)

<代码>val将重载体,因此你可能不得不改成编号。 您可以使用非正常加操作器:

var num1 = +$( #id1 ).val(); // Now you get 5 instead of "5" 
var num2 = +$( #id2 ).val();

//The problem shows up here when you are doing string concatenation instead of addition in the first set of parenthesis
var newVal = (num1+2)*(num2/4);

if(newArr[0]==newVal) // .get is for jQuery collections, just use brackets on an array

Try this

var num1 = 50;   
  var num2 = 100;

  var newVal = (num1+2)*(num2/4);
  var nextVal =(num1+4)*(num2/2);
  var newArr = new Array();
  newArr.push(newVal);
  newArr.push(nextVal);

  newArr.sort();

  if(newArr[0]==newVal)
    $("body").html("<p>works fine!</p>");
  else
    $("body").html("<p>works awfully!</p>");




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签