Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将两个值乘以一个形式_Javascript_Html - Fatal编程技术网

Javascript 将两个值乘以一个形式

Javascript 将两个值乘以一个形式,javascript,html,Javascript,Html,对于我的代码,我尝试将两个参数调用到一个函数中。离开第二个输入框后,将通过的两个数字相乘,并将其放入第三个文本框。如果前两个输入框中的任何一个为空,则将其涂成浅红色。这是到目前为止我的代码,我遗漏了什么 Javascript: function multiply(one, two) { if(one==""){ this.style.color='red'; } if(two==""){ this.style.color='red'; } else{ txt1=one * two; return

对于我的代码,我尝试将两个参数调用到一个函数中。离开第二个输入框后,将通过的两个数字相乘,并将其放入第三个文本框。如果前两个输入框中的任何一个为空,则将其涂成浅红色。这是到目前为止我的代码,我遗漏了什么

Javascript:

function multiply(one, two) {
if(one==""){
this.style.color='red';
}
if(two==""){
this.style.color='red';
}
else{
txt1=one * two;
return txt1;
}

}
HTML5:

First Value: <input type="text" name="mFirst" />
Second Value: <input type="text" name="mLast" onblur="multiply(mFirst.value, mLast.value)" />
Multiplication of First and Second Value: <input type="text" name="answer">
第一个值:
第二个值:
第一个和第二个值的乘积:

您没有将
传递给
乘法()函数

如果要更改
this.style
,可以将
this
作为参数传递

另外,您应该将
mFirst.value
更改为
this.form.elements.mFirst.value
,对于
mLast.value

HTML:


您没有将此
传递给
multiply()
函数

如果要更改
this.style
,可以将
this
作为参数传递

另外,您应该将
mFirst.value
更改为
this.form.elements.mFirst.value
,对于
mLast.value

HTML:


函数乘法(一,二){
如果(一和二){
this.form.elements.answer.value=1*2;
}否则{
这个.style.color='red';
}
}
空字符串是非真实值,因此只有当两个值都不是空字符串时,
one&&two
才会为真

使用
call
可以设置函数中使用的
的值

演示: 您可能需要查看。


函数乘法(一,二){
如果(一和二){
this.form.elements.answer.value=1*2;
}否则{
这个.style.color='red';
}
}
空字符串是非真实值,因此只有当两个值都不是空字符串时,
one&&two
才会为真

使用
call
可以设置函数中使用的
的值

演示: 您可能需要查看。

第一个值:
第二个值:
第一个和第二个值的乘积:
函数乘法()
{
var num_one=document.getElementById('mFirst').value;
var num_two=document.getElementById('mLast').value;
如果(数字一的类型=='number'&&typeof数字二的类型=='number')
{
document.getElementById('answer').value=(parseInt(num_-one)*parseInt(num_-two));
}
其他的
{
document.getElementById('answer').style.color='red';//不推荐使用此方法
}
}
第一个值:
第二个值:
第一个和第二个值的乘积:
函数乘法()
{
var num_one=document.getElementById('mFirst').value;
var num_two=document.getElementById('mLast').value;
如果(数字一的类型=='number'&&typeof数字二的类型=='number')
{
document.getElementById('answer').value=(parseInt(num_-one)*parseInt(num_-two));
}
其他的
{
document.getElementById('answer').style.color='red';//不推荐使用此方法
}
}

使用类似的注释,为什么这不起作用

function monetaryfields()
{
if (multiply) { total.value = unitary.value * quantity.value; }
else { unitary.value = total.value / quantity.value; }
}

<form>
<ul>
<li>Quantity: <input type="text" name="quantity" onblur="monetaryfields.call(true)" /></li>
<li>Unitary: <input type="text" name="unitary" onblur="monetaryfields.call(true)" /></li>
<li>Total: <input type="text" name="total" onblur="monetaryfields.call(false)" /></li>
</ul>
</form>
函数monetaryfields()
{
如果(乘){total.value=unity.value*quantity.value;}
else{unity.value=total.value/quantity.value;}
}
  • 数量:
  • 单一的:
  • 总数:

使用类似的注释,为什么这不起作用

function monetaryfields()
{
if (multiply) { total.value = unitary.value * quantity.value; }
else { unitary.value = total.value / quantity.value; }
}

<form>
<ul>
<li>Quantity: <input type="text" name="quantity" onblur="monetaryfields.call(true)" /></li>
<li>Unitary: <input type="text" name="unitary" onblur="monetaryfields.call(true)" /></li>
<li>Total: <input type="text" name="total" onblur="monetaryfields.call(false)" /></li>
</ul>
</form>
函数monetaryfields()
{
如果(乘){total.value=unity.value*quantity.value;}
else{unity.value=total.value/quantity.value;}
}
  • 数量:
  • 单一的:
  • 总数:

首先,您不能只返回值,您需要将其分配给答案框我想知道…我们在哪里将返回值分配给答案框?phrogz在下面为您找到了它--(this.form.elements.answer.value)首先,您不能只返回值,你需要把它分配给答案框我想知道…我们在哪里把返回值分配给答案框?下面是phrogz为你准备的--(this.form.elements.answer.value)它开始工作了,但有个问题。如何使“txt1”出现在第三个输入框(“应答”)?-1当
one
为空时,此处的逻辑不正确(与OP中的逻辑相同)。此外,这只是设置了一个全局变量。@Phrogz我只需要将焦点放在第二个框上,然后将答案显示在第三个框中,然后将第一个框和第二个框相乘。它开始工作了,但有一个问题。如何使“txt1”出现在第三个输入框(“应答”)?-1当
one
为空时,此处的逻辑不正确(与OP中的逻辑相同)。此外,这只是设置了一个全局变量。@Phrogz我只需要将焦点放在第二个框上,然后将答案显示在第三个框中,然后将第一个框和第二个框相乘。试着用这个值测试一下0@JohnnyHarlamert你自己试试看;有一个演示链接。提示:数字
0
是假的,但是字符串
“0”
是真的。我输入了单词“foo”,得到了NaN。此外,form.elements引用已经过时,无法与HTML5标准保持距离。我也不明白你为什么用call。您还使用如此长的函数引用使DOM膨胀。这太疯狂了!我见过的最糟糕的检查答案是用这个值来测试它0@JohnnyHarlamert你自己试试看;有一个演示链接。提示:数字
0
是假的,但是字符串
“0”
是真的。我输入了单词“foo”,得到了NaN。此外,form.elements引用已经过时,无法与HTML5标准保持距离。我也不明白你为什么用call。您还使用如此长的函数引用使DOM膨胀。这太疯狂了!这是我见过的最糟糕的答案。。。这是有效的:函数calc(multiply){if(multiply){tt.value=parseFloat(qt.value*un.value).toFixed(2);}否则{un.value=parseFloat(tt.value/qt.value).toFixed(2);}
First Value: <input type="text" name="mFirst" id="mFirst" />
Second Value: <input type="text" name="mLast" id="mLast" onblur="multiply()" />
Multiplication of First and Second Value: <input type="text" name="answer" id="answer" />

function multiply()
{
    var num_one = document.getElementById('mFirst').value;
    var num_two = document.getElementById('mLast').value;

    if(typeof num_one === 'number' && typeof num_two === 'number')
    {
        document.getElementById('answer').value = (parseInt(num_one) * parseInt(num_two));
    }
    else
    {
        document.getElementById('answer').style.color = 'red'; // not recommended method
    }




}
function monetaryfields()
{
if (multiply) { total.value = unitary.value * quantity.value; }
else { unitary.value = total.value / quantity.value; }
}

<form>
<ul>
<li>Quantity: <input type="text" name="quantity" onblur="monetaryfields.call(true)" /></li>
<li>Unitary: <input type="text" name="unitary" onblur="monetaryfields.call(true)" /></li>
<li>Total: <input type="text" name="total" onblur="monetaryfields.call(false)" /></li>
</ul>
</form>