输入|将JavaScript函数设置的新值获取到Controller公共函数中 [英] Input | Get the new value set by a JavaScript function into a Controller public function

查看:77
本文介绍了输入|将JavaScript函数设置的新值获取到Controller公共函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CRUD以编辑现有的表行.

I have created a CRUD to edit existing table rows.

它的输入之一已经包含在创建行时保存的值="19.00":

One of its input contain already a value="19.00" saved at the creation of the row :

<input type="number" step="1" title="Prix" class="form-control" name="productprice" id="productprice" value="19.00">

当用户更改选择列表的值时(例如,如果其中一个产品属性更改并影响价格,我将使用javascript函数覆盖CRUD中的值):

I'm overriding the value in my CRUD with a javascript function when the value of a select list is changed by the user, for example if one of the product property is changed and impact the price :

$('#form').on('change', function() {

var d1 = document.getElementById('ordersdetailproducttype_ID').value; // producttypeID
// console.log('producttype ID:', d1);
var d2 = document.getElementById('ordersdetailproductname').value; // productnameID
// console.log('product ID:', d2);
    var d3 = document.getElementById('ordersdetaildessertservingID').value; // dessertservingID
// console.log('Servings:', d3);
var pl1 = document.getElementById('ordersdetailID_portion').value; // partyloafportionID
// console.log('pl1:', pl1);
var pl2 = document.getElementById('ordersdetailpartyloafweightID').value; // partyloafweightID
// console.log('pl2:', pl2);

if (d1 == '' && d2 == '' && d3 == '' && pl1 == '' && pl2 == '') { $('#ordersdetailproductprice').val('0.00'); }
else if (d1 == 1 && d2 == 1 && d3 == 1 && pl1 == '' && pl2 == '') { $('#ordersdetailproductprice').val('19.00'); }
else if (d1 == 1 && d2 == 1 && d3 == 2 && pl1 == '' && pl2 == '') { $('#ordersdetailproductprice').val('24.00'); }
...

在这种情况下,用户会在输入中看到新值(价格)

In this case the user sees the new value (price) into the input,

通过检查元素结构看不到的值:

value which I don't see by inspecting the element structure :

<div class="form-group header-group-0 " id="form-group-productprice" style="display: block;">
<label class="control-label col-sm-2">Prix
        </label>

<div class="col-sm-10">
    <input type="number" step="1" title="Prix" class="form-control" name="productprice" id="productprice" value="19.00">
    <div class="text-danger"></div>
    <p class="help-block"></p>
</div>

如果我用检查器查看元素,我可以看到输入的值仍然是

If I look at the element with the inspector, I can see that the value of the input is still

value="19.00"

我需要检索我的JavaScript函数在控制器中设置的新值,以便能够使用控制器公共函数相应地更新我的表行列.

I need to retrieve the new value set by my JavaScript function in my controller in order to be able to update accordingly my table row column with a controller public function.

我第一次遇到此类问题时,我不知道如何解决此问题.非常感谢您的专业知识.谢谢,马克

First time I'm confronted with this kind of issue, I have no clue how to solve this. Would appreciate your expertise. Thanks, Marc

推荐答案

解决方案在JS脚本中调用Ajax:

SOLUTION AJAX CALL IN JS SCRIPT :

...
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 2 && pl2 == 2) { $('#productprice').val('59.00'); }
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 3 && pl2 == 3) { $('#productprice').val('69.00'); }
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 4 && pl2 == 4) { $('#productprice').val('79.00'); }
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 4 && pl2 == 5) { $('#productprice').val('89.00'); }

var rowID = rowid;
var orderID = $('#orderID').val();
var productprice_new = $('#productprice').val();
console.log(productprice_new,rowID,orderID);

$.ajax({
type: 'post',
url: '/assets/ajax/ajaxupdate.php',
data:  {
        'rowid' : rowid,
        'orderID' : orderID,
        'productprice_new' : productprice_new,
        },
            success: function (data) {
                console.log('worked!');
            },
            error: function (data) {
                console.log('Error:', data);
            }
})

这篇关于输入|将JavaScript函数设置的新值获取到Controller公共函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆