1

我在各种文件中有 jQuery,最近我需要更改母版页中的项目。这导致各种 jaavscript 包括停止工作。Stackoverflow提出了解决通过 ID 选择器获取问题的好主意。

$("#ctl00_ContentMainPane_eliteUser").html

但是我有一个问题,我们使用 jquery.validate.js 来验证表单控件,所以在外部JS 文件中有这样的代码

$(document).ready(function(){ 
    $("#aspnetForm").validate({
        rules: 
    {
        ctl00$ContentMainPane$txtFirstName:
        {
            required:true,
            CheckAlfaNumeric:true
        },
        ctl00$ContentMainPane$ctl00$ucRFI$txtComments:
        {
            required:true
        }                       

    },
    messages:
    {
        ctl00$ContentMainPane$txtFirstName: 
        {
            required:" Please enter first name"
        },
        ctl00$ContentMainPane$ctl00$ucRFI$txtComments:
        {
            required:" Please enter comments."
        }
    }
    });
    $("#" + GetPlaceholder() + "txtFirstName").blur(function(){
            $("#" + GetPlaceholder() + "txtFirstName").valid();
    });
    jQuery.validator.addMethod("CheckAlfaNumeric", function(value, element) {
            return this.optional(element) || /^[A-Za-z\ ]+$/i.test(value);
    }, " Please enter alphabet.");
});

如果名称由于母版页被修改而发生更改,是否知道如何防止属性的命名问题?

4

3 回答 3

5

等待 .NET 4.0,它允许您准确指定 ID 的构造方式;)

Seriously: you can AFAIK create your rules manually, doing something like (a JS object is nothing but an "array" of properties):

var myRules = new Object();
myRules[GetPlaceholder() + "txtFirstName"] = { required:true, CheckAlfaNumeric:true };

var myMessages = new Object();
myMessages[GetPlaceholder() + "txtFirstName"] = { required:"Please enter first name" };

$("#aspnetForm").validate({ rules: myRules, messages: myMessages });
于 2009-08-05T11:36:25.143 回答
1

Have you looked at http://weblogs.asp.net/psperanza/archive/2009/05/07/jquery-selectors-selecting-elements-by-a-partial-id.aspx for partial matches in jQuery.
The only other option I can see is to add the js file contents to the page and use something <%=txtFirstName.ClientID%>

于 2009-08-05T11:37:37.480 回答
0

My answer is my modification of veggerby. Please upvote his response, not mine. He saved the day.

I'm putting my response here to add a slight caveat. (I'm doing the Using 'attr("name")). And to show how to do it with a drop down list.

没有看到的是我的内容页面有一个名为“ddlState”和“ddlFavoriteColor”的 asp.net 下拉列表。我在下拉列表项的顶部放置了一个虚拟的“--Select--”值,所以我想要 selectedIndex 大于零的东西。

请注意,这个项目在 3.5 中“卡住”了。:< 我宁愿用很酷的 4.0 功能做点什么。

是的,我的“检查”代码可能会更小,但是对于网络示例,我喜欢稍微冗长而不是简洁,以显示正在发生的事情。

function GetStateDropDownName() {
    var cntlName = $("[id$=_ddlState]").attr("name");
    return cntlName;
}

function GetFavoriteColorDropDownName() {
    var cntlName = $("[id$=_ddlFavoriteColor]").attr("name");
    return cntlName;
}

$(document).ready(function () {

    jQuery.validator.addMethod("dropdownBoxHasItemSelected", function (value, select, arg) {
        var returnValue = false;
        var selectedIndex = $(select).prop("selectedIndex");
        if (selectedIndex != 0) {
            returnValue = true;
        }
        return returnValue;
    }, "Please select a item.");


    var myRules = new Object();
    myRules[GetStateDropDownName()] = { dropdownBoxHasItemSelected: true };
    myRules[GetFavoriteColorDropDownName()] = { dropdownBoxHasItemSelected: true };

    var myMessages = new Object();
    myMessages[GetStateDropDownName()] = { dropdownBoxHasItemSelected: "Please select a State." };
    myMessages[GetFavoriteColorDropDownName()] = { dropdownBoxHasItemSelected: "Please select a Favorite Color." };


    // Initialize validation on the entire ASP.NET form.
    $("#aspnetForm").validate({
        rules: myRules, messages: myMessages
    });
于 2013-03-28T15:01:44.893 回答