Browse Source

开发金额字段js表单规则并修改

antshop
linyaostalker 5 years ago
parent
commit
290addcc6f
  1. 22
      backend/modules/goods/views/goods/express.php
  2. 46
      backend/modules/goods/views/goods/goods.php
  3. 29
      backend/modules/shop/views/express-template/express_area_form.php

22
backend/modules/goods/views/goods/express.php

@ -52,8 +52,28 @@ use backend\modules\shop\models\ars\ExpressTemplate;
$(this).val("0.00") $(this).val("0.00")
} }
var basicPrice = $(this).val(); var basicPrice = $(this).val();
$(this).val(basicPrice.toString().match(/^\d+(?:\.\d{0,2})?/));
$(this).val(toFixeds($(this).val(), 2));
}) })
//金额补全
function toFixeds(val, pre) {
const num = parseFloat(val);
// eslint-disable-next-line no-restricted-globals
if (isNaN(num)) {
return false;
}
const p = 10 ** pre;
const value = num * p;
let f = (Math.round(value) / p).toString();
let rs = f.indexOf('.');
if (rs < 0) {
rs = f.length;
f += '.';
}
while (f.length <= rs + pre) {
f += '0';
}
return f;
}
JS; JS;
$this->registerJs($js); $this->registerJs($js);

46
backend/modules/goods/views/goods/goods.php

@ -62,3 +62,49 @@ use backend\modules\goods\models\ars\Goods;
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?> <?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
<?= Html::a('返回', ['index'], ['class' => 'btn btn-info']) ?> <?= Html::a('返回', ['index'], ['class' => 'btn btn-info']) ?>
</div> </div>
<?php
$js =<<<JS
$("#goods-market_price").blur(function(){
if(isNaN($(this).val())){
$(this).val("0.00")
}
if($(this).val().indexOf('-') != -1){
$(this).val("0.00")
}
var basicPrice = $(this).val();
$(this).val(toFixeds($(this).val(), 2));
})
$("#goods-price").blur(function(){
if(isNaN($(this).val())){
$(this).val("0.00")
}
if($(this).val().indexOf('-') != -1){
$(this).val("0.00")
}
var basicPrice = $(this).val();
$(this).val(toFixeds($(this).val(), 2));
})
//金额补全
function toFixeds(val, pre) {
const num = parseFloat(val);
// eslint-disable-next-line no-restricted-globals
if (isNaN(num)) {
return false;
}
const p = 10 ** pre;
const value = num * p;
let f = (Math.round(value) / p).toString();
let rs = f.indexOf('.');
if (rs < 0) {
rs = f.length;
f += '.';
}
while (f.length <= rs + pre) {
f += '0';
}
return f;
}
JS;
$this->registerJs($js);
?>

29
backend/modules/shop/views/express-template/express_area_form.php

@ -86,6 +86,26 @@ function changeCalType(type){//当切换计算方式
} }
calType = type; calType = type;
} }
//金额补全
function toFixeds(val, pre) {
const num = parseFloat(val);
// eslint-disable-next-line no-restricted-globals
if (isNaN(num)) {
return false;
}
const p = 10 ** pre;
const value = num * p;
let f = (Math.round(value) / p).toString();
let rs = f.indexOf('.');
if (rs < 0) {
rs = f.length;
f += '.';
}
while (f.length <= rs + pre) {
f += '0';
}
return f;
}
$(document).ready(function(){ $(document).ready(function(){
$("#expressarea-basic_count").blur(function(){ $("#expressarea-basic_count").blur(function(){
if(isNaN($(this).val())){ if(isNaN($(this).val())){
@ -96,8 +116,7 @@ $(document).ready(function(){
$(this).val(1) $(this).val(1)
} }
var basiccount = $(this).val(); var basiccount = $(this).val();
// $(this).val(Math.floor(basiccount * 10) / 10);
$(this).val(basiccount.toString().match(/^\d+(?:\.\d{0,1})?/));
$(this).val(toFixeds($(this).val(), 1));
} else{ } else{
if($(this).val() < 1){ if($(this).val() < 1){
$(this).val(1) $(this).val(1)
@ -114,7 +133,7 @@ $(document).ready(function(){
$(this).val("0.00") $(this).val("0.00")
} }
var basicPrice = $(this).val(); var basicPrice = $(this).val();
$(this).val(basicPrice.toString().match(/^\d+(?:\.\d{0,2})?/));
$(this).val(toFixeds($(this).val(), 2));
}) })
$("#expressarea-extra_count").blur(function(){ $("#expressarea-extra_count").blur(function(){
if(isNaN($(this).val())){ if(isNaN($(this).val())){
@ -125,7 +144,7 @@ $(document).ready(function(){
$(this).val(0) $(this).val(0)
} }
var basiccount = $(this).val(); var basiccount = $(this).val();
$(this).val(Math.floor(basiccount * 10) / 10);
$(this).val(toFixeds($(this).val(), 1));
} else{ } else{
if($(this).val() < 0){ if($(this).val() < 0){
$(this).val(0) $(this).val(0)
@ -142,7 +161,7 @@ $(document).ready(function(){
$(this).val("0.00") $(this).val("0.00")
} }
var basicPrice = $(this).val(); var basicPrice = $(this).val();
$(this).val(basicPrice.toString().match(/^\d+(?:\.\d{0,2})?/));
$(this).val(toFixeds($(this).val(), 2));
}) })
$("input:radio[name='ExpressArea[calculation_type]']").on('ifChecked', function(event){ $("input:radio[name='ExpressArea[calculation_type]']").on('ifChecked', function(event){

Loading…
Cancel
Save