如何在Jquery验证中处理html元素ID/名称中的特殊字符? [英] How to handle special characters in html element id/name in Jquery validation?
问题描述
我有一个HTML表单,该表单在ID(:,-,_)中使用特殊字符.该表单使用JQuery验证插件来验证用户输入.具体来说,该ID包含一个GUID, 以下是示例代码:
I have a HTML form which uses special characters in ids ( :,-,_ ). The form uses JQuery validation plugin to validate the user input. Specifically the id includes a GUID, Following is the sample code:
<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-validate/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#respondForm').validate({ onclick: false,
onkeyup: false,
onfocusout: false,
highlight:
function(element, errorClass) {
$(element).css({ backgroundColor: '#FFFF88' });
}
,
errorLabelContainer: $("ul", $('div.error-container')),
wrapper: 'li',
rules: { Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405:
{
required: true
, minlength: 5
, maxlength: 10
}
}
,
messages: { firstName:
{
required: "xxx_Required"
, minlength: "XXX Should be greater than 5"
, maxlength: "XXX Cannot be greater than 10"
}
}
});
});
</script>
</head>
<body>
<form action="/Home/Submit" method="post" id="respondForm">
<div>
<div class="error-container">
<ul>
</ul>
</div>
</div>
<div>
<input type="text" id="Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405" name="Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405" />
<br />
<input type="submit" name="submit" value="Submit" />
<br />
</div>
</form>
您可以看到输入类型的文本元素的ID/名称为"Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405
"
As you can observe input type text element has the id/name as "Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405
"
如果我在浏览器中运行此页面,则会收到JavaScript错误:
If I run this page in the browser, I receive a JavaScript error:
网页错误详细信息
Webpage error details
消息:应为'}'
线:23
字符:50
代码:0
URI: http://localhost:64603/
Message: Expected '}'
Line: 23
Char: 50
Code: 0
URI: http://localhost:64603/
错误指向此行:
function(element, errorClass) {
$(element).css({ backgroundColor: '#FFFF88' });
}
,
errorLabelContainer: $("ul", $('div.error-container')),
wrapper: 'li',
***[Line 23] -----> rules: { Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405:***
我该如何解决?
推荐答案
新月形"您的建议有效,将ID用单引号引起来:
"Crescent Fresh" your suggestion worked, wrapping the id in single quotes:
rules: { 'Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405':
{
使其正常工作
这篇关于如何在Jquery验证中处理html元素ID/名称中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!