gpt4 book ai didi

javascript - 如何在 Jquery 验证中处理 html 元素 id/name 中的特殊字符?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:22:57 25 4
gpt4 key购买 nike

我有一个 HTML 表单,它在 ID 中使用特殊字符 (:,-,_)。该表单使用 JQuery 验证插件来验证用户输入。具体来说,id 包括一个 GUID,以下是示例代码:

<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

如果我在浏览器中运行这个页面,我会收到一个 JavaScript 错误:

Webpage error details

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:***

我该如何解决?

最佳答案

"Crescent Fresh"你的建议奏效了,用单引号将 id 括起来:

rules: { 'Input:_CDD66FA6-D190-434D-AF51-8272F64E0646_14ecbb3f-c0e0-4caf-b03a-013d12118405':
{

让它工作

关于javascript - 如何在 Jquery 验证中处理 html 元素 id/name 中的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com