我的问题似乎很简单,但确实令人困惑。我有HTML的测试输入,但是有FreeMarker。每次从键盘输入符号时,JS脚本都会运行,以指定是按键事件还是字母。问题是,如果您想通过按Alt + z输入特定字母,它会按Ctrl + z的形式工作,然后将其放入。
脚本:
function validateInputValue(event, id, regexpValue) {
var regexp = new RegExp(regexpValue);
var key = event.keyCode || event.which;
//var fieldValue = jQuery("#" + id).val();
// Allow: backspace, delete, tab, enter, arrow keys
var controlKeys = [8, 46, 9, 13, 37, 38, 39, 40];
// Ctrl+ anything or one of the conttrolKeys is valid
var isControlKey = event.ctrlKey || controlKeys.join(",").match(new RegExp(key));
if (isControlKey && !event.altKey) {
return;
}
if (!regexp.test(String.fromCharCode(key))) {
event.preventDefault();
}
}
我已经在if语句中添加了“!event.altKey”,以使用alt键阻止所有事件,但是现在当按下Alt + z时,它不会运行。调试器不会在断点处停止,但它仍可作为“撤消”命令使用。它可以与其他特殊字母(例如Alt + s)配合使用。有什么办法可以使其正常工作?
整个FreeMarker标签:
<#macro text property style="" styleClass="" label="" labelText="" labelStyle="" id=genId(property) labelPosition="left" readonly="false" disabled="false" required="false" maxlength="" onfocus="" onkeypress="" onkeyup="" onblur="" onchange="" onclick="" associatedLabel="true" maxLengthLive="" type="TEXT" trAttributes="" validatedField="" valueFormat="" onEnterPressButton="" formatDecimalJs="false" formatDecimalPrecision="null" dateFormat="dd-MM-yyyy" regexp="" htmlAttr="" placeholder="">
<@assertInContainer name="form" message="Text control must be placed inside form container." />
<@labeledComponent label=label style=labelStyle styleClass="${styleClass}" id=id position=labelPosition required=required disabled=disabled associatedLabel=associatedLabel labelText=labelText trAttributes="${trAttributes}">
<#if validatedField!="">
<@spring.bind "command." + validatedField />
<#if spring.status.errorMessages?size gt 0>
<#local class=" puc_error ui-state-error " />
<#elseif readonly="true">
<#local class=" puc_form_readonly " />
<#else>
<#local class="" />
</#if>
</#if>
<@spring.bind "command." + property />
<#if spring.status.errorMessages?size gt 0>
<#local class=" puc_error ui-state-error " />
<#elseif readonly="true">
<#local class=" puc_form_readonly " />
<#else>
<#local class="" />
</#if>
<#if readonly="true">
<#local rohtml="readOnly=\"readOnly\"" />
<#else>
<#local rohtml="" />
</#if>
<#if disabled="true">
<#local dishtml="disabled" />
<#local class=" puc_form_disabled " />
<#else>
<#local dishtml="" />
</#if>
<#if maxlength!="">
<#local maxlengthhtml="maxlength=\"" + maxlength + "\"" />
<#else>
<#local maxlengthhtml="" />
</#if>
<#if spring.status.value?? && valueFormat!="" && spring.status.value?is_number>
<#local valueFormattedString=spring.status.value?double?string(valueFormat)?default("") />
<#else>
<#local valueFormattedString=spring.status.value?default("")?html />
</#if>
<#if !spring.status.value?? && spring.status.actualValue??>
<#if !spring.status.actualValue?is_date >
<#local valueFormattedString=spring.status.actualValue?html />
<#else>
<#local valueFormattedString=spring.status.actualValue?string(dateFormat)/>
</#if>
</#if>
<#if onEnterPressButton!="">
<#local localonkeyup="onEnterPressButton(event,'" + onEnterPressButton + "');" + onkeyup />
<#else>
<#local localonkeyup=onkeyup />
</#if>
<#local onBlurHtml="" />
<#if formatDecimalJs=="true">
<#local onBlurHtml="formatujWartoscDecimal($(this)," + formatDecimalPrecision + ");" />
</#if>
<#local onBlurHtml=onBlurHtml+onblur />
<#if regexp!="">
<#local localonkeypress="validateInputValue(event, '" + id + "', '" + regexp + "');" + onkeypress />
<#else>
<#local localonkeypress=onkeypress />
</#if>
<#if placeholder!="">
<#local placeholderhtml="placeholder=\"" + placeholder + "\"" />
<#else>
<#local placeholderhtml="" />
</#if>
<input name="${spring.status.expression}" value="${valueFormattedString}" style="${style}" id="${id}" class="${class} puc_form_input" ${rohtml} ${dishtml} ${maxlengthhtml} onkeypress="${localonkeypress}" onkeyup="${localonkeyup}"
onblur="${onBlurHtml}" onchange="${onchange}" onclick="${onclick}" onfocus="${onfocus}" ${htmlAttr} ${placeholderhtml}
<#if type=="TEXT">
type="text"
<#elseif type="EMAIL">
type="email"
<#elseif type="PASSWORD">
type="password"
</#if>
<#if required="true">
required="required"
</#if>
/>
<script type="text/javascript">
//<![CDATA[
function validateInputValue(event, id, regexpValue) {
var regexp = new RegExp(regexpValue);
var key = event.keyCode || event.which;
//var fieldValue = jQuery("#" + id).val();
// Allow: backspace, delete, tab, enter, arrow keys
var controlKeys = [8, 46, 9, 13, 37, 38, 39, 40];
// Ctrl+ anything or one of the conttrolKeys is valid
var isControlKey = event.ctrlKey || controlKeys.join(",").match(new RegExp(key));
debugger;
if (isControlKey && !event.altKey) {
return;
}
if (!regexp.test(String.fromCharCode(key))) {
event.preventDefault();
}
}
//]]>
</script>
</@labeledComponent>
</#macro>