javascript - How do I test a variable passed into a function? - Stack Overflow

Quick question, I'm trying to pass a value to a variable and then run a function if the variable w

Quick question, I'm trying to pass a value to a variable and then run a function if the variable was set to something specific. How do I do that?

I have the following example where I want to assign the value name to a form field if the variable type is set to 1.

function test(name,type) {
  if (type=1) {
    document.myform.name.value = name;
  }
}

and a link in the body of the html:

<a href="javascript:test('myname','1');">FILL IN MY NAME</a>

I also tried the following with no luck:

function test(name,type) {
  var checktype = type;
  if (checktype = 1) {
    document.myform.name.value = name;
  }
}

I'm pretty sure something like this is possible, just not sure what I'm doing wrong.

Quick question, I'm trying to pass a value to a variable and then run a function if the variable was set to something specific. How do I do that?

I have the following example where I want to assign the value name to a form field if the variable type is set to 1.

function test(name,type) {
  if (type=1) {
    document.myform.name.value = name;
  }
}

and a link in the body of the html:

<a href="javascript:test('myname','1');">FILL IN MY NAME</a>

I also tried the following with no luck:

function test(name,type) {
  var checktype = type;
  if (checktype = 1) {
    document.myform.name.value = name;
  }
}

I'm pretty sure something like this is possible, just not sure what I'm doing wrong.

Share asked Mar 3, 2010 at 12:00 ChoyChoy 2,11711 gold badges39 silver badges49 bronze badges 1
  • thanks to everyone for the responses. I knew it had to be something simple. I also discovered on my own that if I try to set the value of the variable to something other than a number, say type == go, I need to place the value in quotes. – Choy Commented Mar 3, 2010 at 19:16
Add a ment  | 

8 Answers 8

Reset to default 3

Try this:

function test(name,type) {
  var checktype = type;
  if (checktype == 1) {
    document.myform.name.value = name;
  }
}

You were missing ==

You want to use ==, not =. Using a single symbol will assign the value of 1 to the variable checktype, rather than testing for equality:

function test(name,type) {
  if (type == 1) {
    document.myform.name.value = name;
  }
}

To avoid this type errors try to do this..

function test(name,type) {
  if (1 == type) {
    document.myform.name.value = name;
  }
}

In this case, if you type by mistake, 1=type, then you get an error and you locate it right way.

If variable didn't pass to function argument, the argument address will be Null. Therefore you need to check the addresses with if statement inside your function. ie:

bool flag(int & value)
{
  if (&value == NULL) {
      return false;
  }
  else {
      return true;
  }
}

Javascript uses == for equality checks.

function test(name,type) {
  if (type === "1") {
    document.myform.name.value = name;
  }
}

No need to use an additional variable inside the function for storing the value. Also use ===' instead of==` if you know the type of the variable.

See Strict equal

Returns true if the operands are strictly equal (see above) with no type conversion.

You need to use a parison operator instead of an assignment operator:

function test(name, type) {
  if (type == 1) {
    document.myform.name.value = name;
  }
}

If you’re using an assignment operator, the whole expression evaluates to the assigned value. That means type = 1 evaluates to 1 that is equal true and the branching condition is always fulfilled.

function test(name,type) {
  if (type==1) {
    document.myform.name.value = name;
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745071815a4609595.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信