简体   繁体   English

如何访问表单选择jQuery? 如何从输入的金额中获取“%”?

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

I want to built a fee calculator, and for that I need to access forms, I wondered if I can do it in jQuery. 我想构建一个费用计算器,为此,我需要访问表格,我想知道是否可以在jQuery中进行操作。 So my code is that : 所以我的代码是:

<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>

And the JS : 和JS:

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

Here is my problem. 这是我的问题。 I want to know how to grab the amount that the user entered in the input and add to this amount of 10%, then print it in the paragraph below. 我想知道如何获取用户在输入中输入的金额并将其添加到10%的金额中,然后在下面的段落中进行打印。

First off all , add id to your input like this 首先,像这样将id添加到您的输入中

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

Now get the value like this: 现在获得这样的值:

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

Don't use spaces in your ID 请勿在ID中使用空格

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

Use this instead 改用这个

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

Now you can add 10% to the amount like this 现在,您可以像这样添加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/ http://jsfiddle.net/mohammadAdil/E2rJQ/15/

Use the # sign for id. 使用#号作为ID。 Also add an id to the input. 还要在输入中添加一个ID。 id="feeInput"

Also title is not a valid input tag. 另外,标题不是有效的输入标签。

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

Try this 尝试这个

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);
}

Heres a demo: http://jsfiddle.net/Ln3RN/ 这是一个演示: http : //jsfiddle.net/Ln3RN/

I changed the output id and the selector. 我更改了输出ID和选择器。 example in jsfiddle jsfiddle中的示例

attribute selectors 属性选择器

$(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 returns false so that the form would not submit, only trigger onsubmit event. getFee返回false,以便表单不会提交,仅触发onsubmit事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM