1

我重新发布这个是为了让我的问题更具体。我正在尝试结合此处找到的计算器:http ://www.nhpta.com/over-tax-calculator.html 。

我希望计算按钮足够聪明,可以知道两个输入字段之一是否输入了值,然后执行两个 JS 函数之一,Calculate 或 Calculate2。如果两个字段都输入了值,我希望它代替按钮抛出错误。这是我下面的概念代码,我不知道如何定义 php 变量,也不知道如何告诉它查看每个输入字段并确定是否输入了值。也不确定打印是否正确?

<?php
    $input_B   = "form field named input_B";
    $input_C  = "form field named input_C";

    if($input_B == 'has a value' && $input_C == 'has no value')
    { 
        print ' <P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onClick="Calculate(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"></P>'; 
    } 
    elseif($input_C == 'has a value' && input_B == 'has no value' )
    { 
        print ' <P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onClick="Calculate2(this.form.input_D.value, this.form.input_E.value, this.form.input_F.value, this.form)"></P>';
    } 
    elseif ($input_C == 'has a value' && input_B == 'has a value')
    { 
        print ' Please choose only one field to fill in';
    } 
    else 
    {
        print ' <P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onClick="Calculate(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"></P>';
    } // End if/else if
?> 
4

3 回答 3

0

As long as the two input fields have (unique!) ids, you can do something like this:

var field0 = document.getElementById("field0id").value;
var field1 = document.getElementById("field1id").value;
if(field0 === ""){
    if(field1 !== ""){
        calculate();
    }else{
        //error
    }
}elseif(field1 === ""){
    calculate2();
}else{
    //error
}
于 2012-08-03T14:01:38.507 回答
0

First of all, you shouldn't need to do anything server-side for this (that's where PHP runs). Instead use javascript. It should look something like this: (Bear in mind that this is using jQuery, which is a javascript library. You can learn about jQuery here.)

<label for="input1">Input 1</label>
<input type="text" name="input1" id="input1" /><br />
<label for="input2">Input 2</label>
<input type="text" name="input2" id="input2" /><br />
<input type="submit" value="Submit" id="submit" onclick="calculate();" <!-- you only need the onclick attribute if you're not using jQuery --> />

Edit: functions modified to perform the logic as defined in the question

<!-- pseudo-code with jQuery -->
<script language="javascript" type="text/javascript">
    $("#submit").on('click', function() {
        var input1Val = $("#input1").val();
        var input2Val = $("#input2").val();

        if (input1Val != "" && input2Val != "") {
            alert("You may only enter a value in one input field.");
        } else if (input1Val != "") {
            calcFunc1(input1Val);
        } else if (input2Val != "") {
            calcFunc2(input2Val);
        } else {
            alert("You must enter a value in one of the input fields.");
        }
    });
</script>

<!-- pseudo-code with straight javascript -->
<script language="javascript" type="text/javascript">
    function calculate() {
        var input1Val = document.getElementById("input1").value;
        var input2Val = document.getElementById("input1").value;

        if (input1Val != "" && input2Val != "") {
            alert("You may only enter a value in one input field.");
        } else if (input1Val != "") {
            calcFunc1(input1Val);
        } else if (input2Val != "") {
            calcFunc2(input2Val);
        } else {
            alert("You must enter a value in one of the input fields.");
        }
    }
</script>
于 2012-08-03T14:02:32.827 回答
0

第一个 if 语句应该这样重写:

if(($input_B != "") && ($input_C == "")) 

那么其他的逻辑都是一样的

于 2012-08-03T14:00:11.527 回答