MeteorJS:如何获取输入文本的值

时间:2023-01-28 17:59:10

I have this code:

我有这个代码:

product.jade

template(name="product")
    label(for="qty") Qty:
    input#qty.form-control(type="text", value="1", name="qty")
    button.btn.btn-default.addcart Add to Cart

product.coffee

Template['product'].events
  'click .addcart': (event, template) ->
    ????

How do I get the value of input text qty? I tried the event variable but its limited in the button. Any ideas?

如何获取输入文本数量的值?我尝试了事件变量,但它在按钮中有限。有任何想法吗?

1 个解决方案

#1


2  

Considering your code you can get the value like this:

考虑到您的代码,您可以获得如下值:

'click .addcart': (event, template) ->
    qty = template.find('#qty').value;

You can see here the documentation about template.find().

您可以在这里看到有关template.find()的文档。

But if you have a submit event on your <form> you can also do this:

但是如果你的

上有提交事件,你也可以这样做:

'submit .your-form': (event, template) -> 
    qty = event.target.qty.value //qty = name of the field

#1


2  

Considering your code you can get the value like this:

考虑到您的代码,您可以获得如下值:

'click .addcart': (event, template) ->
    qty = template.find('#qty').value;

You can see here the documentation about template.find().

您可以在这里看到有关template.find()的文档。

But if you have a submit event on your <form> you can also do this:

但是如果你的

上有提交事件,你也可以这样做:

'submit .your-form': (event, template) -> 
    qty = event.target.qty.value //qty = name of the field