0

我在运行时使用 <%= %> 语法评估了一段 javascript 代码...

现在,在 <%= %> 内部,我希望将值存储在变量中,而不是硬编码字符串……我该怎么做?

这是功能:

function updateDescriptionLabel(msgToParse, dataColumn, TextBoxID) {
    var myData = JSON.parse(msgToParse.d);
    alert(msgToParse);
    alert(dataColumn);
    alert(TextBoxID);

    // this is the explicit call, it's ok
    $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));

    // NOW i want to make the call by using the variable value
    var txtDes = TextBoxID;

    $('#' + '<%= this.' + txtDes.toString() + '.ClientID %>').val($.trim(myData["DescrMacch"]));

    // BUT i get the error: Too many characters in character literal
}

============== 编辑===============

我有一堆 TextBoxID,在失去焦点时,从数据库中获取一个值,并将其显示在与 ID 相关的适当 TextBoxDESCRIPTION 上......但我必须为每个 TextBox 复制代码,所以我想概括它...

我发布了整个代码。

 <script language="javascript" type="text/javascript">



    /* ==> JSON  */

    //Ajax Request
    function SendAjaxRequest(urlMethod, jsonData, returnFunction) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: urlMethod,
            data: jsonData,
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.
                if (msg != null) {
                    returnFunction(msg);
                }
            },
            error: function (xhr, status, error) {
                // Boil the ASP.NET AJAX error down to JSON.
                var err = eval("(" + xhr.responseText + ")");

                // Display the specific error raised by the server
                alert(err.Message);
            }
        });
    }

    // I'd like to generalize it ...
    function SendComplexAjaxRequest(urlMethod, jsonData, returnFunction, dataColumn, TextBoxID) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: urlMethod,
            data: jsonData,
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.
                if (msg != null) {
                    returnFunction(msg, dataColumn, TextBoxID);
                }
            },
            error: function (xhr, status, error) {
                // Boil the ASP.NET AJAX error down to JSON.
                var err = eval("(" + xhr.responseText + ")");

                // Display the specific error raised by the server
                alert(err.Message);
            }
        });
    }




// ONE function for each textBox
    function callUpdateGuastoAttributes(code) {
        var urlMethod = '<% = ResolveClientUrl("~/Services/ws_Attributes.asmx/OdLGetMacchinaAttributes") %>';
        var jsonData = "{'COD_MACCHINA':'" + code + "'}";
        var successFunction = updateLabelsGuastoAttributs;
        SendAjaxRequest(urlMethod, jsonData, successFunction);
    }

    function callUpdateCausaGuastoAttributes(code) {
        var urlMethod = '<% = ResolveClientUrl("~/Services/ws_Attributes.asmx/OdLGetMacchinaAttributes") %>';
        var jsonData = "{'COD_MACCHINA':'" + code + "'}";
        var successFunction = updateLabelsCausaGuastoAttributs;
        SendAjaxRequest(urlMethod, jsonData, successFunction);
    }


// I can have only one function:
    function callUpdateMacchinaAttributes(code) {
        var urlMethod = '<% = ResolveClientUrl("~/Services/ws_Attributes.asmx/OdLGetMacchinaAttributes") %>';
        var jsonData = "{'COD_MACCHINA':'" + code + "'}";
        var successFunction = updateDescriptionLabel;
        SendComplexAjaxRequest(urlMethod, jsonData, successFunction, 'DescrMacchina', 'TextBoxDES_MACCHINA');
    }





    /* <== CALLBACK  */
function updateLabelsMacchinaAttributs(msg) {
        var myData = JSON.parse(msg.d);
        $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));
    }

    function updateLabelsGuastoAttributs(msg) {
        var myData = JSON.parse(msg.d);
        $('#' + '<%= this.TextBoxDES_GUASTO.ClientID  %>').val($.trim(myData["description"]));
    }

    function updateLabelsCausaGuastoAttributs(msg) {
        var myData = JSON.parse(msg.d);
        $('#' + '<%= this.TextBoxDES_CAUSA_GUASTO.ClientID  %>').val($.trim(myData["description"]));
    }

// BUT I have to generalize it...
function updateDescriptionLabel(msgToParse, dataColumn, TextBoxID) {
        var myData = JSON.parse(msgToParse.d);
        alert(msgToParse);
        alert(dataColumn);
        alert(TextBoxID);

        // this is the explicit call
        $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));

        // i want to make the call by using the variables values
        var dCol = dataColumn;
        var txtDes = TextBoxID;

        $('#' + '<%= this.' + txtDes.toString() + '.ClientID %>').val($.trim(myData[dCol]));

        // i get the error: Too many characters in character literal
    }
4

1 回答 1

0

我提出这样的解决方案(取决于你的其他代码......):

function updateDescriptionLabel(msgToParse, dataColumn, TextBoxID) {
    var myData = JSON.parse(msgToParse.d);
    alert(msgToParse);
    alert(dataColumn);
    alert(TextBoxID);

    // this is the explicit call, it's ok
    $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));

    // NOW i want to make the call by using the variable value

    $('#' + TextBoxID).val($.trim(myData["DescrMacch"]));
}

然后当你调用这个函数时这样做:

updateDescriptionLabel("your message", <yourcolumn>, <%= this.TextBoxDES_MACCHINA.ClientID  %>);
于 2013-04-23T09:37:34.030 回答