如果不在JavaScript中使用parseFloat,则无法正确添加两个数字(Cannot add two numbers correctly without using parseFloat in JavaScript)
我遇到了这个问题。 它使用parseFloat或parseInt添加数字。 如果textbox1的值是4和textbox2的值是2那么我得到的输出为(见脚本)
我的疑问是为什么另外单独
parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())
给出正确的价值,但
parseFloat($('#txt1').val() + $('#txt2').val())
没有给出正确的价值,而
parseFloat($('#txt1').val() - $('#txt2').val())
,parseFloat($('#txt1').val() / $('#txt2').val())
,parseFloat($('#txt1').val() * $('#txt2').val())
正在给予正确的价值。 它很简单,但我找不到解决方案。
=====的jQuery
function Calculate() { //--> Output $('#lbl1').html(parseFloat($('#txt1').val() + $('#txt2').val())); //--> 42 $('#lbl2').html(parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())); //--> 6 $('#lbl3').html(parseFloat(4 + 2)); //--> 6 $('#lbl4').html(parseFloat($('#txt1').val() - $('#txt2').val())); //--> 2 $('#lbl5').html(parseFloat($('#txt1').val() * $('#txt2').val())); //--> 8 $('#lbl6').html(parseFloat($('#txt1').val() / $('#txt2').val())); //--> 2 }
===== HTML
<table> <tr> <td> <input type="text" id="txt1" /> </td> <td> <input type="text" id="txt2" /> </td> </tr> <tr> <td> <input type="button" value="Calculate" onclick="Calculate()" /> </td> <td> <label id="lbl1"> </label> | <label id="lbl2"> </label> | <label id="lbl3"> </label> | <label id="lbl4"> </label> | <label id="lbl5"> </label> | <label id="lbl6"> </label> </td> </tr> </table>
I came across this problem. It adding numbers using parseFloat or parseInt. IF textbox1 value is 4 and textbox2 value is 2 then i got output as (see script)
My doubt is why in addition alone
parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())
gives correct value but
parseFloat($('#txt1').val() + $('#txt2').val())
is not giving correct value whereas
parseFloat($('#txt1').val() - $('#txt2').val())
,parseFloat($('#txt1').val() / $('#txt2').val())
,parseFloat($('#txt1').val() * $('#txt2').val())
are giving correct value. Its simple but i couldn't find solution.
=====jQuery
function Calculate() { //--> Output $('#lbl1').html(parseFloat($('#txt1').val() + $('#txt2').val())); //--> 42 $('#lbl2').html(parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())); //--> 6 $('#lbl3').html(parseFloat(4 + 2)); //--> 6 $('#lbl4').html(parseFloat($('#txt1').val() - $('#txt2').val())); //--> 2 $('#lbl5').html(parseFloat($('#txt1').val() * $('#txt2').val())); //--> 8 $('#lbl6').html(parseFloat($('#txt1').val() / $('#txt2').val())); //--> 2 }
=====HTML
<table> <tr> <td> <input type="text" id="txt1" /> </td> <td> <input type="text" id="txt2" /> </td> </tr> <tr> <td> <input type="button" value="Calculate" onclick="Calculate()" /> </td> <td> <label id="lbl1"> </label> | <label id="lbl2"> </label> | <label id="lbl3"> </label> | <label id="lbl4"> </label> | <label id="lbl5"> </label> | <label id="lbl6"> </label> </td> </tr> </table>
原文:https://stackoverflow.com/questions/16117985
最满意答案
实际上,你可以在Blaze空间栏中使用部分:
- 单个参数,充当被调用模板的数据上下文 (如“ 流星指南”>“视图”>“用户界面”>“UI组件”>“智能组件”中所示 )。
- 命名参数(如Meteor Guide> View> Blaze> Spacebars> Template inclusion ),它们实际上组装成一个数据对象(见下文)。
{{> subComponent arg1="value-of-arg1" arg2=helperThatReturnsValueOfArg2}}
另一个教程: http : //meteorcapture.com/spacebars/#template-inclusion-ex-2
包含标记(
{{> foo}}
)和块标记({{#foo}}
)采用单个数据参数或无参数。 任何其他形式的参数都将被解释为对象规范或嵌套助手 :
对象规范 :如果只有关键字参数,如
{{#with x=1 y=2}}
或{{> prettyBox color=red}}
,则关键字参数将被组合成一个数据对象,其属性以关键字。嵌套助手:如果有一个位置参数后跟其他(位置或关键字参数),则使用正常的助手参数调用约定在其他参数上调用第一个参数。
然后通过
templateInstance.data
属性检索数据上下文:
在
onCreated
,onRendered
或onDestroyed
回调中,模板实例可直接在onDestroyed
使用。在帮助器或模板的HTML部分中,数据上下文可直接在此处使用(无需查找其
data
子属性)。 在帮助程序中,您还可以通过Template.instance()
访问模板。在事件处理程序中,模板实例作为侦听器的第二个参数传递。
Indeed, you can use partials in Blaze spacebars with either:
- A single argument that acts as the data context of the called template (as exemplified in Meteor Guide > View > User Interfaces > UI components > Smart components).
- Named arguments (as shown in Meteor Guide > View > Blaze > Spacebars > Template inclusion), which are actually assembled into a data object (see below).
{{> subComponent arg1="value-of-arg1" arg2=helperThatReturnsValueOfArg2}}
Another tutorial: http://meteorcapture.com/spacebars/#template-inclusion-ex-2
Reference: Meteor API Docs > Packages > spacebars > Inclusion and Block Arguments
Inclusion tags (
{{> foo}}
) and block tags ({{#foo}}
) take a single data argument, or no argument. Any other form of arguments will be interpreted as an object specification or a nested helper:
Object specification: If there are only keyword arguments, as in
{{#with x=1 y=2}}
or{{> prettyBox color=red}}
, the keyword arguments will be assembled into a data object with properties named after the keywords.Nested Helper: If there is a positional argument followed by other (positional or keyword arguments), the first argument is called on the others using the normal helper argument calling convention.
Then you retrieve the data context through the
templateInstance.data
property:
In an
onCreated
,onRendered
oronDestroyed
callback, the template instance is directly available inthis
.In a helper or in the HTML part of the template, the data context is directly available in
this
(no need to look for itsdata
child property). In a helper, you can also access the template throughTemplate.instance()
.In an event handler, the template instance is passed as the 2nd argument of the listener.
相关问答
更多-
Meteor:如何使用Spacebars应用不同的CSS元素?(Meteor: How can I use Spacebars to apply different CSS elements?)[2023-05-14]
你只需要一个帮助器来返回适当的类: Template.YOURTEMPLATE.helpers({ headerClass: function () { var class = { 'ToDo': 'teal-header', 'Info': 'orange-header' }[this.type] return class || 'pink-header' } }) 然后在你的模板中:如Meteor文档中所述,模板代码中的this.subscribe将在模板实例被销毁时自动取消订阅。 如果您需要, Meteor.subscribe需要明确取消订阅。 使用其中一个的决定取决于您的应用程序结构。 如果您确定数据仅与给定模板相关,则使用模板作用域订阅,即this.subscribe 。 如果数据在多个页面中使用,则使用“全局”表单,或者使用更高模板级别(通过页面保留的模板级别,例如布局)。 As explained in Meteor documentation, this.subscribe ...实际上,你可以在Blaze空间栏中使用部分: 单个参数,充当被调用模板的数据上下文 (如“ 流星指南”>“视图”>“用户界面”>“UI组件”>“智能组件”中所示 )。 命名参数(如Meteor Guide> View> Blaze> Spacebars> Template inclusion ),它们实际上组装成一个数据对象(见下文)。 {{> subComponent arg1="value-of-arg1" arg2=helperThatReturnsValueOfArg2}} 另一个教程: http ...你需要两个助手:一个用于返回房屋,另一个用房子作为上下文返回邻居。 如果没有模式,我无法给出准确的答案,但我们假设每个邻居都有一个连接两者的houseId属性。 Template.monitoring.helpers({ houses: function () { return house.find(); }, neighbors: function () { // Note the context here is a house because it's // cal ...如果我理解正确,您有两个问题 - 将year传递给您的month助手,并使用该参数使您的查询更具体 - 是吗? 要将year传递给帮助程序,请按照{{#each thing in things}}约定( 此处说明)更改代码。 所以你的模板代码将成为:-
{{#each year in getYears}}
-
{{year}}
-
...
最好在帮助程序级别执行此操作,例如,这会向posts帮助程序添加可选的索引参数: Template.home.helpers({ posts(index) { if(typeof index !== "undefined" && index !== null){ var data = Posts.find(); return data[index]; } else{ return ...在_.each循环中,您正在访问错误的数据项。 您也可以使用_.each循环而不是外部for循环。 如果你这样做: let contacts = Contacts.find().fetch(); _.each(contacts, function(contact) { _each(contact.contact, function(c) { data.push( { "email": c.emailAddress, ...由此产生的HTML无效,因为多个元素具有相同的id -attribute。 事件处理程序很容易解释。 event-map中的键被分为event-type和可选的event-target 。 event-target传递给jQuery,jQuery容忍id的无效使用。 所以jQuery使用id player为所有元素添加事件监听player 。 Matt K很好地解释了其余部分。 The HTML produced by this is invalid, due to multiple elements ha ...Huumm,关于Blaze的新闻允许new创建变量(使用#let)。 简单的解决方案是: let objects = [{name: 'John', age: 18}, {name: 'Foo', age: 25}, {name: 'Bar', age: 35}] let attrs = ['name', 'age'] {{#each objects}} {{#let object=this object_idx=@index}}Object {{object_idx}}
...我不熟悉AngularJS,但这里有一个如何使用Meteor完成此任务的示例。 此示例显示了一个人员列表,以及可用于按年龄过滤显示列表的HTML编号输入。 client/views/persons/persons.html-
{{#each personsFiltered}}
{{> person}}
{{/ ...
相关文章
更多- javascript 问题
- javascript的验证
- Memcached add 添加key-value命令(存在不会更新)
- 请教javascript问题
- javascript 注释
- javascript 基本语法
- JavaScript 事件
- JavaScript 事件
- 使用 JavaScript 实现对 PDF 的全文索引
- 在HTML中使用javascript
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)