繁体   English   中英

使用MVC 5将值传递到文本框

[英]Pass value to a textbox using mvc 5

我需要将值传递给文本框,但用户无法直接写入此文本框。 我正在使用jason计算值:

<script type="text/javascript">
    $(document).ready(function () {
        $("#txtid").change(function () {
            $.ajax({
                url: '/CommissionsFinals/GetTarifa',
                type: 'GET',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                data: { clientID: $("#DDLCliente").val(), consultorID: $("#DDLConsultor").val() },

                success: function (tarifa) {
                    //response(tarifa.
                    $('#txtid').val(tarifa).autocomplete

                },
                error: function () {
                    alert('Error!');
                }
            });

        })
    });

当我尝试输入不同的值时,文本框会更改其值。 该值由2个不同的DropDownLists中选择的2个参数(clientId,consultorId)计算得出。 从DropDownList中选择这两个值后,如何自动获得该值?

<th>
    @Html.DropDownListFor(m =>m.ManagerId, new SelectList(ViewBag.Manager, "ManagerId", "NomeCompleto"), "Seleccione o Manager", new { @class = "form-control", @id = "DDLManager" })
</th>
<th>
    @Html.Label("Consultor", htmlAttributes: new { @class = "control-label col-md-2" })
</th>
<th>
    @Html.DropDownListFor(m => m.ConsultorId, new SelectList(ViewBag.Consultor, "ConsultorId", "NomeCompleto"), "Seleccione o Consultor", new { @class = "form-control", @id = "DDLConsultor" })
</th>

是否完全调用了来自ajax的成功回调? $('#txtid')。val(tarifa)应该可以工作,但是可能未到达该代码。 您可以使用断点/警报或某项进行测试。 除此之外,我发现我的ajax调用有一个区别,我通常使用类型:“ POST”,这对我有用。

您可以使用此方法来处理Javascript中的下拉更改事件

$("#test").change(function() {
    alert( "do you stuff" );
});

并检查是否选中了两个下拉列表,最后调用ajax(jquery)方法将所选值提交给控制器。

目前尚不清楚您想做什么。 如果您询问如何从下拉列表中获取选定的值,则可以执行此操作。

    $('#ConsultorId').on('change', function() {
        var consultorId = $('#ConsultorId').val();
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM