0

我在 Jquery-UI-Dialog 中有表单。我了解如何验证表单中的所有字段。使用本站插件

例子:

$(document).ready(function () {

    $('#dialog').validate({
        rules: {
            category:
            {
                required: true
            },
        },
        submitHandler: function (form) { // for demo
            if ($('#ccategory').val() == 0){
                document.getElementById('choose_own_text').innerHTML  = "Please change here";
                return false;}
         //   console.log($('#ccategory').val());
            alert('valid form'); // for demo
            return false; // for demo
        }
    });

});

但我有我通过Javascript创建的idand 。name例如id="inputp"+j+"_id",其中 j 是一个数字。

现在我想验证这个字段,但我有错误:

SyntaxError: missing : after property id

我使用这种方法是因为我用数字声明了数据。

如何更正此错误,我可以始终使用此技术的验证。

 $( "#wnd_Add" ).dialog({
                autoOpen: false,
                height: 'auto',
                width: 350,
                modal: true,
                resizable:false,
                buttons: {
                    "Add": function() {
                    var j=(parseInt(contapara)+1);
                    $('#formparam').validate({
                    rules: {
                    "inputp"+j+"_id":
                    {
                     required: true
                     },
                     },
                     submitHandler: {
                                     contapara=(parseInt(contapara)+1);

                    document.getElementById("sorpara").innerHTML+="<li id=\"inputp"+contapara+"_id\" class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>"+$('#inputp'+contapara+'_id').val()+"</li>";   

                    var $fm = $("#formparam");
                    $.get($fm.attr('${nextstep}'))
                        .done(function(data, ok){
                        var fr=$fm.serialize();
                        fr=fr.replace(/\&/g,"+%23+");
                        fr=replacew(fr,/\=/g, "%24+"); // //fr=fr.replace(/\=/g,"%24+");
                                        if(document.getElementById("inputp"+contapara+"_visible").checked==false)
                                          fr=fr+"+%23+inputp"+contapara+"_visible%24+off";
                        // data is the content of the response
                        document.location.href="index2?"+fr;})
                        .fail(function(data){
                        alert('call failed');
                        // call failed for some reason -- add error messaging?
                        });

    //                                 $( this ).dialog( "close" ); 

                                     return false;  },
                                     },

                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                },
                close: function() {
                    $( this ).dialog( "close" );
                }
            });

我在Jsfiddle上创建了一个演示但没有运行,因为我有这个错误但可能会有所帮助

我希望问题的标题是正确的......

更新:

SyntaxError: missing : after property id现在我有这个脚本, 但我有错误

function(form)    {

这是最后一个脚本:

$( "#wnd_Addparam" ).dialog({
                autoOpen: false,
                height: 'auto',
                width: 350,
                modal: true,
                resizable:false,
                buttons: {
                    "Add": function() {
                    var j=(parseInt(contapara)+1);
                    var rules= {};
                    rules["inputp" + j + "_id"]=
                    {
                     required: true
                     };
                    $('#formparam').validate({
                    rules: rules,
                     submitHandler: {
                     function(form)    {
                                     contapara=(parseInt(contapara)+1);

                    document.getElementById("sorpara").innerHTML+="<li id=\"inputp"+contapara+"_id\" class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>"+$('#inputp'+contapara+'_id').val()+"</li>";   

                    var $fm = $("#formparam");
                    $.get($fm.attr('${nextstep}'))
                        .done(function(data, ok){
                        var fr=$fm.serialize();
                        fr=fr.replace(/\&/g,"+%23+");
                        fr=replacew(fr,/\=/g, "%24+"); // //fr=fr.replace(/\=/g,"%24+");
                                        if(document.getElementById("inputp"+contapara+"_visible").checked==false)
                                          fr=fr+"+%23+inputp"+contapara+"_visible%24+off";
                        // data is the content of the response
                        document.location.href="index2?"+fr;})
                        .fail(function(data){
                        alert('call failed');
                        // call failed for some reason -- add error messaging?
                        });

                                     return false; } },
                                     });
                                     },

                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                },
                close: function() {
                    $( this ).dialog( "close" );
                }
            });
4

1 回答 1

1

您不能将变量用作这样的对象中的键,而是可以执行类似的操作

var rules = {};
rules["inputp"+j+"_id"] = {
    required: true
};
$('#formparam').validate({
    rules: rules,
    submitHandler: {

前任;

var contapara = 3;
var regex, v, l, c, b;
$("#wnd_Addparam").dialog({
    autoOpen : false,
    height : 'auto',
    width : 350,
    modal : true,
    resizable : false,
    buttons : {
        "Add" : function() {
            var j = (parseInt(contapara) + 1);

            var rules = {};
            rules["inputp" + j + "_id"] = {
                required : true
            };

            $('#formparam').validate({
                rules : rules,
                submitHandler : function(form) {
                    contapara = (parseInt(contapara) + 1);

                    document.getElementById("sorpara").innerHTML += "<li id=\"inputp"
                            + contapara
                            + "_id\" class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>"
                            + $('#inputp' + contapara + '_id').val() + "</li>";

                    var $fm = $("#formparam");
                    $.get($fm.attr('${nextstep}')).done(function(data, ok) {
                        var fr = $fm.serialize();
                        fr = fr.replace(/\&/g, "+%23+");
                        fr = replacew(fr, /\=/g, "%24+"); // //fr=fr.replace(/\=/g,"%24+");
                        if (document.getElementById("inputp" + contapara
                                + "_visible").checked == false)
                            fr = fr + "+%23+inputp" + contapara
                                    + "_visible%24+off";
                        // data is the content of the response
                        document.location.href = "index2?" + fr;
                    }).fail(function(data) {
                        alert('call failed');
                            // call failed for some reason -- add error
                            // messaging?
                        });
                    return false;
                }
            })
        },

        Cancel : function() {
            $(this).dialog("close");
        }
    },
    close : function() {
        $(this).dialog("close");
    }
});

$("#btn_Addpar").click(function() {
    i = (parseInt(contapara) + 1);
    $("#formparam").remove();
    $("#wnd_Addparam")
            .append('<form method="GET" name="formparam"  id="formparam" action="${nextstep}">\
    <input type="hidden" name="json_data" value="${json_data}">\
    <input type="hidden" name="tag" value="${tag}">\
    <table><tr><td><label>ID</label></td><td>\
    <textarea class="expand" name="inputp'
                    + i
                    + '_id" id="inputp'
                    + i
                    + '_id"></textarea></td></tr>\
    <tr><td><label>Type</label></td><td><select name="inputp'
                    + i
                    + '_type" id="inputp'
                    + i
                    + '_type">\
    <option value="text">Text</option><option value="integer">Integer</option><option value="float">Float</option>\
    <option value="list_values">List of values</option><option value="range">Range</option>\
    <option value="selection_collapsed">Selection (collapsed)</option>\
    <option value="selection_expanded">Selection (expanded)</option>\
    <option value="subimage">Subimage selection</option>\
    <option value="polygon">Polygon selection</option>\
    <option value="horizontal_separator">Horizontal separator</option>\
    </select></td></tr><tr><td><label> Description</label></td>\
    <td><textarea class="expand" name="inputp'
                    + i
                    + '_description" id="inputp'
                    + i
                    + '_description"></textarea></td></tr>\
    <tr><td><label>Value</label></td><td><textarea class="expand" name="inputp'
                    + i
                    + '_value" id="inputp'
                    + i
                    + '_value"></textarea></td></tr>\
    <tr><td><label>Info (help)</label></td><td><textarea class="expand" id="inputp'
                    + i
                    + '_info" name="inputp'
                    + i
                    + '_info"></textarea></td></tr>\
    <tr><td><label> Visible?</label></td><td><input type="checkbox" id="inputp'
                    + i
                    + '_visible" name="inputp'
                    + i
                    + '_visible" checked></td></tr></table></form>');

    $("#wnd_Addparam").dialog("open");
});
于 2013-06-27T13:52:42.133 回答