首页 \ 问答 \ 如果不在JavaScript中使用parseFloat,则无法正确添加两个数字(Cannot add two numbers correctly without using parseFloat in JavaScript)

如果不在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
更新时间:2022-06-03 18:06

最满意答案

实际上,你可以在Blaze空间栏中使用部分:

{{> subComponent arg1="value-of-arg1" arg2=helperThatReturnsValueOfArg2}}

另一个教程: http//meteorcapture.com/spacebars/#template-inclusion-ex-2

参考: Meteor API文档>包>空格键>包含和块参数

包含标记( {{> foo}} )和块标记( {{#foo}} )采用单个数据参数或无参数。 任何其他形式的参数都将被解释为对象规范嵌套助手

  • 对象规范 :如果只有关键字参数,如{{#with x=1 y=2}}{{> prettyBox color=red}} ,则关键字参数将被组合成一个数据对象,其属性以关键字。

  • 嵌套助手:如果有一个位置参数后跟其他(位置或关键字参数),则使用正常的助手参数调用约定在其他参数上调用第一个参数。

然后通过templateInstance.data属性检索数据上下文:

  • onCreatedonRenderedonDestroyed回调中,模板实例可直接在onDestroyed使用。

  • 在帮助器或模板的HTML部分中,数据上下文可直接在此处使用(无需查找其data子属性)。 在帮助程序中,您还可以通过Template.instance()访问模板。

  • 在事件处理程序中,模板实例作为侦听器的第二个参数传递。


Indeed, you can use partials in Blaze spacebars with either:

{{> 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 or onDestroyed callback, the template instance is directly available in this.

  • In a helper or in the HTML part of the template, the data context is directly available in this (no need to look for its data child property). In a helper, you can also access the template through Template.instance().

  • In an event handler, the template instance is passed as the 2nd argument of the listener.

相关问答

更多