English 中文(简体)
使用通过的百分比有问题 (“ 输入”) val ()
原标题:Having problems using percentages passed in through ("input").val()

说实话,我认为我只需要将数值转换成整数,但我尝试了一堆事情, 并不断获得 NaN 或一些无法在对象上使用此动作的线条。 我想做一些事情, 比如用替换来删除% 或简单地将数学应用到它。 我尝试了字符串和剖析Int () 来尝试转换, 但只要得到 NaN 即可 。

这个示例给了我一个提示框, 显示“ 10% ” 或“ 20% ” 。 无论用户类型在什么 。

thisInvoiceDiscount = $("#clientSearchInvoiceDiscountTextbox").val(); 
percentPresent = thisInvoiceDiscount.indexOf("%"); 
if (percentPresent == "-1") { 
} 
else { 
  alert (thisInvoiceDiscount);
  //I can t seem to do anything with thisInvoiceDiscount here
}

更新:使用第1次答复:

thisInvoiceDiscount = $("#clientSearchInvoiceDiscountTextbox").val();
var numberOnly = $("#clientSearchInvoiceDiscountTextbox").val().replace(/D/g,   );
percentPresent = thisInvoiceDiscount.indexOf("%");
if (percentPresent == "-1") {
}
else {
    var integerValue =  parseInt(numberOnly, 10);
    alert (integerValue);
}
最佳回答
var numberOnly = 
        $("#clientSearchInvoiceDiscountTextbox").val().replace(/D/g,   );

这将从字符串中剔除每一个非数字字符串。

var integerValue =  parseInt(numberOnly, 10);

这将将字符串解析为整数 。

您当然可以让 regex 更具体地针对 符号 :

var numberOnly = 
        $("#clientSearchInvoiceDiscountTextbox").val().replace(/%/,   );

或此 regex 仅当它是字符串中最后一个字符时才删除 :

var numberOnly = 
        $("#clientSearchInvoiceDiscountTextbox").val().replace(/%$/,   );

"http://jsfiddle.net/RQDtH/1/" rel="无尾随"\\\\ enough>Live DEMO (根据最新情况) (根据最新情况)


< 强 > 注意 < / 强 >, 当比较 < code> indexof 的结果时, 您最好比一字符串而不是一字符串, 它会在您的代码中添加一个( 非常小的... ) 加速度, 如果您比较了 < code\\\/ code >, 该代码将不刹车

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.