簡體   English   中英

如何訪問表單選擇jQuery? 如何從輸入的金額中獲取“%”?

[英]how to access form choose jQuery ? How to grab “%” from the amount that entered?

我想構建一個費用計算器,為此,我需要訪問表格,我想知道是否可以在jQuery中進行操作。 所以我的代碼是:

<form id="fee">
    <input type="text" title="fee" placeholder="Place the amount that you would like to send"/> $
    <input type="submit" onclick="getFee()"/>
</form>
<br/>
<p id="Here will be the fee"></p>

和JS:

function getFee(){
    $("fee > input:fee").
}

這是我的問題。 我想知道如何獲取用戶在輸入中輸入的金額並將其添加到10%的金額中,然后在下面的段落中進行打印。

首先,像這樣將id添加到您的輸入中

<input type="text" id="amount"

現在獲得這樣的值:

 var amount = $("#amount").val();

請勿在ID中使用空格

<p id="Here will be the fee"></p>

改用這個

<p id="feeOnAmount"></p>

現在,您可以像這樣添加10%的金額

function getFee(){
    var amount = parseFloat($("#amount").val());
    if($.isNumeric(amount)){
        $("#feeOnAmount").html((amount * 1.1));    
    }
    else{
        $("#feeOnAmount").html("please enter a valid number");
    }
}

http://jsfiddle.net/mohammadAdil/E2rJQ/15/

使用#號作為ID。 還要在輸入中添加一個ID。 id="feeInput"

另外,標題不是有效的輸入標簽。

function getFee(){
        $("#fee > input#feeInput").
    }

嘗試這個

function getFee(){
    var inputVal = $("#fee > input[title='fee']").val();
    var inputFinal = parseInt(inputVal) + (parseInt(inputVal) * .10);

    //Change the ID of the p your appending to
    //ID is now = "calc"
    $("#calc").text(inputFinal);
}

這是一個演示: http : //jsfiddle.net/Ln3RN/

我更改了輸出ID和選擇器。 jsfiddle中的示例

屬性選擇器

$(document).ready(function () {
    $("#fee")[0].onsubmit= getFee;

});
function getFee(){
        var feeInput = $('#fee > input[title="fee"]').val();
        feeInput = parseInt(feeInput);
        $('#Here_will_be_the_fee').text(feeInput*1.1);
        return false;
}

getFee返回false,以便表單不會提交,僅觸發onsubmit事件。

暫無
暫無

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

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