Javascript变量作为链中的函数名

时间:2011-02-10 17:12:47

标签: javascript function variables chaining

我试图将从表单元素捕获的变量拉到函数链中。变量值需要充当函数名称。例如:

<form name="myform">
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="monday" />Monday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="tuesday" />Tuesday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="wednesday" />Wednesday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="thursday" />Thursday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="friday" />Friday
</form>

脚本:

var **monthday**=$("input[name='DayOfWeek']:checked").val();
NewDate = Date.parse(NewDate).add(1).month().**monthday**();

我的问题是将变量'monthday'值作为该链中的函数名称。我有一个“呃”的时刻还是不可能?

2 个答案:

答案 0 :(得分:2)

这个怎么样:Date.parse(NewDate).add(1).month()[monthday]();

答案 1 :(得分:1)

使用bracket notation

var monthday = $("input[name='DayOfWeek']:checked").val();
NewDate = Date.parse(NewDate).add(1).month()[monthday]();