Javascript 变量作为链中的函数名称

发布于 2024-10-16 20:16:43 字数 762 浏览 0 评论 0原文

我正在尝试将从表单元素捕获的变量拉入函数链。变量值需要充当函数名称。示例:

<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”值充当该链中的函数名称。我是否有一个“呃”时刻或者这是不可能的?

I am trying to pull a variable that is captured from a form element in to a function chain. The variable value needs to act as a function name. Example:

<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>

script:

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

My problem has been getting the variable 'monthday' value to act as the function name in that chain. Am I having a 'duh' moment or is this not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-10-23 20:16:43

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

How about this:Date.parse(NewDate).add(1).month()[monthday]();?

影子是时光的心 2024-10-23 20:16:43

使用括号表示法

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

Use bracket notation:

var monthday = $("input[name='DayOfWeek']:checked").val();
NewDate = Date.parse(NewDate).add(1).month()[monthday]();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文