如何将select和运行这个函数onload?
How to select and run this function onload?
所以我的脚本中有这个工作代码块,用于将小数点分隔符从逗号“,”替换为句点“。” , 编辑表单时。因为在这个区域中,小数点分隔符逗号是正常的,所以我也希望值像这样显示 1,99€ 所以我恢复了工作功能。所选字段应在加载时更改。提交表格后,我会再次将其退回。对于此示例,我只向您展示其中一个字段。
value="1.5" 以错误的方式从 Magento 后端加载,这是另一回事:
我包括了onload:"function(event)"
和 window.onload = function();
展示我从 jQuery 解决此功能的两次尝试: jQuery('form').on('change', '#price', function(event)
我还需要知道如何删除 .on('change' 部分。第一次使用 Js和 jQuery。我真的什么都试过了。
<html>
<body onload="function(event)">
<form>
<input id="price" value="1.5">
</form>
</body>
</html>
<script>
window.onload = function();
jQuery('form').on('change', '#price', function(event) {
event.preventDefault();
if (jQuery('#price').val().includes('.')) {
var varwithpoint = jQuery('#price').val();
varwithcomma = varwithcomma.replace(",",".");
jQuery('#price').val(varwithpoint);
}
else {
console.log('no dot to replace');
}
});
</script>
代码中有几部分似乎没有按预期工作,因此下面是一个将“,”转换为“.”的基本代码示例。如果存储在输入 "price" 中,并在每次更改值后检查它;
function convert_price(){
var this_price = $("#price").val();
if (this_price.includes(',')) {
this_price = this_price.replace(",",".");
$('#price').val(this_price);
} else {
console.dir('no dot to replace');
}
}
convert_price();
$("#price").on("change",convert_price);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<html>
<body>
<form>
<input id="price" value="1,5">
</form>
</body>
</html>
我调用了 "init" 将 change
事件附加到输入文件的函数,我还更改了传递给 on
函数的参数
function init(){
var input = jQuery('#price');
input.on('change', function(event) {
event.preventDefault();
var valueInInput = input.val();
if (valueInInput.includes('.')) {
var varwithcomma = valueInInput.replace(",",".");
input.val(varwithcomma);
} else {
console.log('no dot to replace');
}
});
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body onload="init()">
<form>
<input id="price" value="1.5">
</form>
</body>
</html>
所以我的脚本中有这个工作代码块,用于将小数点分隔符从逗号“,”替换为句点“。” , 编辑表单时。因为在这个区域中,小数点分隔符逗号是正常的,所以我也希望值像这样显示 1,99€ 所以我恢复了工作功能。所选字段应在加载时更改。提交表格后,我会再次将其退回。对于此示例,我只向您展示其中一个字段。
value="1.5" 以错误的方式从 Magento 后端加载,这是另一回事:
我包括了onload:"function(event)"
和 window.onload = function();
展示我从 jQuery 解决此功能的两次尝试: jQuery('form').on('change', '#price', function(event)
我还需要知道如何删除 .on('change' 部分。第一次使用 Js和 jQuery。我真的什么都试过了。
<html>
<body onload="function(event)">
<form>
<input id="price" value="1.5">
</form>
</body>
</html>
<script>
window.onload = function();
jQuery('form').on('change', '#price', function(event) {
event.preventDefault();
if (jQuery('#price').val().includes('.')) {
var varwithpoint = jQuery('#price').val();
varwithcomma = varwithcomma.replace(",",".");
jQuery('#price').val(varwithpoint);
}
else {
console.log('no dot to replace');
}
});
</script>
代码中有几部分似乎没有按预期工作,因此下面是一个将“,”转换为“.”的基本代码示例。如果存储在输入 "price" 中,并在每次更改值后检查它;
function convert_price(){
var this_price = $("#price").val();
if (this_price.includes(',')) {
this_price = this_price.replace(",",".");
$('#price').val(this_price);
} else {
console.dir('no dot to replace');
}
}
convert_price();
$("#price").on("change",convert_price);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<html>
<body>
<form>
<input id="price" value="1,5">
</form>
</body>
</html>
我调用了 "init" 将 change
事件附加到输入文件的函数,我还更改了传递给 on
函数的参数
function init(){
var input = jQuery('#price');
input.on('change', function(event) {
event.preventDefault();
var valueInInput = input.val();
if (valueInInput.includes('.')) {
var varwithcomma = valueInInput.replace(",",".");
input.val(varwithcomma);
} else {
console.log('no dot to replace');
}
});
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body onload="init()">
<form>
<input id="price" value="1.5">
</form>
</body>
</html>