簡體   English   中英

如何使復選框更改值?

[英]How to make a checkbox change value?

因此,我只是想知道,只要有人單擊復選框,我將如何更改該值。 基本上, 土地總轉讓稅(PLTT + TLTT)應該變為$0 我已經厭倦了帶復選框的if語句,但是程序無法正常運行。 請我只需要一些建議就可以了嗎?

<!doctype html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    </head>
    <body>
        <table bgcolor="#D4D4D4" border="1" width="450" height="60">
            <tr>
                <td width="175" align="center">Land Transfer Tax Calculator</td>
            </tr>
        </table>

        <table border="0" bgcolor="#E7E7E7" width="450" height="69">
            <tr>
                <td height="30" align="center">Enter the Price:<input type="textbox" name="x1" id="price" style="width:150px"></td><br>
            </tr>
            <tr>
                <td align="center" ><font size="2">(no commas or decimals)</font></td>
            </tr>
            <tr>
                <td align="center"><input type="checkbox"  id="check1" value="">First time home buyers</td>
            </tr>
            <tr>
                <td align="center"><input type="button" id="calculate" style="" value="Calculate"></td>
            </tr>
        </table>

        <table bgcolor="#E7E7E7" width="450" border="0" >
            <tr>
                <td align="center">Provincial Land Transfer Tax (PLTT)</td>
                <td align="center">Toronto Land Transfer Tax (TLTT)</td>
                <td align="center">Total Land Transfer Tax (PLTT + TLTT)</td>
            </tr>
            <tr>
                <td align="center"><input type="textbox" id="provincal" value="" style="width:135px" disabled/></td>
                <td align="center"><input type="textbox" id="toronto" value="" style="width:135px" disabled/></td>
                <td align="center"><input type="textbox" id="final" value="" style="width:135px" disabled></td>
            </tr>
        </table>
    </body>

    <script type="text/javascript">
        $("#calculate").click(function () {
            var x1 = $("#price").val();
            var y1,y2,y3,y4;

            if (x1 <= 50000) {
                y1=x1*0.005; //Provinical
                y2=x1*0.005; //Toronto
                y3=y1+y2;
            }
            else if (x1 <= 250000) {
                y1=(x1*0.01)-275; //Provinical
                y2=y1; //Toronto
                y3=y1+y2;
            }
            else if (x1<=400000) {
                y1=(x1*0.015)-1525; //Provinical
                y2=(x1*0.01)-275; //Toronto
                y3=y1+y2;
            }
            else if (x1>=400001) {
                y1=(x1*0.02)-3525;
                y2=y1-750;
                y3=y1+y2;
            }

            $("#rebate").val(y4);
            $("#final").val(y3);
            $("#toronto").val(y2);
            $("#provincal").val(y1);
        });
    </script>
</html>

您如何執行if語句?

在計算結束時嘗試此操作(但在更改文本框值之前)

if ($("#check1").is(":checked")) {
    y3=0;
}

使用click事件處理程序來處理選中/未選中的事件,並以apt身份運行內部計算。 jsFiddle鏈接

$("#check1").click(function () {
     if ($("#check1").prop("checked")) {
         $("#final").val(0);
         $("#toronto").val(0);
         $("#provincal").val(0);
     }
     else{
         calc();
     }
 });

你可以試試看。

$("checkbox#check1").on('click',function(){
    if($(this).is(":checked")){
         $("input#price").val(0);

    }else{
        calculate();
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM