Javascript 在提交时从查询中删除美元符号
我在一个网站上工作,那里有一个带有字段和“捐款”按钮的捐款箱Javascript 在提交时从查询中删除美元符号,javascript,php,jquery,forms,Javascript,Php,Jquery,Forms,我在一个网站上工作,那里有一个带有字段和“捐款”按钮的捐款箱 <div id="donate-bar"> <form name="donate" id="donate"> <span class="donate-text">DONATE</span> <input type="text" class="amount" name="amount" id="amount" value="$10" size=
<div id="donate-bar">
<form name="donate" id="donate">
<span class="donate-text">DONATE</span>
<input type="text" class="amount" name="amount" id="amount" value="$10" size="5">
<input type="submit" id="contribute" formaction="https://website.com/donate" accept-charset="utf-8" formmethod="GET" value="CONTRIBUTE">
</form>
</div>
捐赠
但是,查询将作为https://website.com/donate?amount=%2410
并且不勾选$10框。如果我删除$
并再次提交查询,则会发送值,并在第二页上看到正确的金额
是否有一种简单的方法可以使用PHP或Javascript在提交表单时删除$
,从而https://website.com/donate?amount=%2410
更改为https://website.com/donate?amount=10
我试过几件事,但都没用。一些代码示例将非常有用,因为我对其中一些代码比较新。我可以在提交时将其解析为整数吗?任何帮助都将不胜感激。谢谢大家! 为什么不将美元符号放在输入之外,并将值存储为“10”:
$_POST['amount'] = str_replace("$", "", $_POST['amount']);
捐赠
$
有一种简单的方法可以解决这个问题 首先,我的建议之一是将字段更改为
type="number"
其次,在PHP中,可以使用以下方法删除美元符号:
$amount = str_replace('$', '', $_REQUEST['amount']);
您可以在jquery中使用split函数,如下所示 var值=($('#amount').val()).split('$); 风险价值金额=价值[1] 在PHP中 $value=explode(“$”,$_POST['amount'])//结果数组元素 不带$符号$金额=$值[1] 或者也可以使用PHP $amount=preg_replace(“/[^0-9]/”,“,$”POST['amount']) //仅获取整数部分,并忽略所有其他部分 也在PHP中 $amount=str_replace(“$”,“$”,POST['amount'])
希望这有帮助最简单的方法是隐藏字段:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
$(document).ready(function() {
//number regex - the format $10
var regex = /\$[1-9][0-9]+/;
//Setting validations
$(document).on('blur', '#pretty_amount', function() {
if(!regex.test($(this).val())) {
//Do something probably clear field
alert("Error Invalid number: " + $(this).val());
} else {
var newValue = $(this).val().match(/[1-9][0-9]+/g);
$('#amount').val(newValue);$('#amount').innerHtml(newValue);
}
});
});
</script>
</head>
<body>
<div id="donate-bar">
<form name="donate" id="donate">
<span class="donate-text">DONATE</span>
<input type="text" style="display: none;" name="amount" id="amount">
<input type="text" class="amount" name="pretty_amount" id="pretty_amount" value="$10" size="6">
<input type="submit" id="contribute" formaction="https://website.com/donate" accept-charset="utf-8" formmethod="GET" value="CONTRIBUTE"></form></div>
</body>
</html>
$(文档).ready(函数(){
//数字正则表达式-格式为$10
var regex=/\$[1-9][0-9]+/;
//设置验证
$(文档).on('blur','pretty#u amount',函数(){
if(!regex.test($(this.val())){
//做点什么,可能是清理场地
警报(“错误无效数字:”+$(this.val());
}否则{
var newValue=$(this.val().match(/[1-9][0-9]+/g);
$('金额').val(newValue);$('金额').innerHtml(newValue);
}
});
});
捐赠
如果我没弄错的话,这正是你想要的
致以最诚挚的问候“我已经尝试了几件事”,比如…?string.replace(“$”,“)-。-如果您不必担心遗留问题,可以使用html5,只需执行
$
。检查此帖子请不要在没有radix
参数的情况下使用parseInt
。。。这是教别人的坏习惯,也应该在PHP中检查;不要相信客户,工作做得很好!谢谢我知道我可能应该使用一个PHP补丁来提高兼容性,但如果JS被禁用,它只会转到一个捐赠页面,在那里他们将不得不再次输入金额。万分感谢@user3791008这是PHP中的一个非常小的变化,第一个示例有点过时了。为什么在第三个示例执行op希望的操作时创建阵列,而微性能却较低?
$amount = str_replace('$', '', $_REQUEST['amount']);
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
$(document).ready(function() {
//number regex - the format $10
var regex = /\$[1-9][0-9]+/;
//Setting validations
$(document).on('blur', '#pretty_amount', function() {
if(!regex.test($(this).val())) {
//Do something probably clear field
alert("Error Invalid number: " + $(this).val());
} else {
var newValue = $(this).val().match(/[1-9][0-9]+/g);
$('#amount').val(newValue);$('#amount').innerHtml(newValue);
}
});
});
</script>
</head>
<body>
<div id="donate-bar">
<form name="donate" id="donate">
<span class="donate-text">DONATE</span>
<input type="text" style="display: none;" name="amount" id="amount">
<input type="text" class="amount" name="pretty_amount" id="pretty_amount" value="$10" size="6">
<input type="submit" id="contribute" formaction="https://website.com/donate" accept-charset="utf-8" formmethod="GET" value="CONTRIBUTE"></form></div>
</body>
</html>