2013-04-11 72 views
0

我想建立一个费用计算器,为此我需要访问表单,我想知道我是否可以在jQuery中完成。 所以我的代码是:如何访问表单选择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%的金额,然后将其打印在下面的段落中。

+0

未来,请尽量在您的问题上更加一致,并提供您正在尝试做什么的完整说明。 – 2013-04-11 20:33:41

回答

1

首先关闭所有,添加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/

+1

您可以将算术简化为'(金额* 1.1)' – 2013-04-11 20:30:50

+0

非常感谢!帮助我很多! 我怎么可以澄清,用户只输入数字? – 2013-04-11 20:35:45

+0

你应该使用'parseFloat'而不是'parseInt'。尝试:'parseInt(010);'。 – andlrc 2013-04-11 20:45:33

0

使用#号标识。还要为输入添加一个ID。 id="feeInput"

此标题不是有效的输入标签。

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

试试这个

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/

0

我改变了输出ID和选择器。 example in 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返回false,以便在窗体不会提交,只会触发onsubmit事件。

相关问题