【发布时间】:2014-06-14 09:55:18
【问题描述】:
使用这段 jQuery,我检查某些字段是否匹配或不为空,但出现此错误。
未捕获的类型错误:无法读取未定义的属性“匹配”
谁能告诉我我在这里做错了什么?
if ( width.match( /^\d+$/ ) && height.match( /^\d+$/ ) && type.length > 0 && color.length > 0 ) {
这是完整的代码:
if( $( "#config" ) ) {
$( 'input, select' ).on( 'change', function(){
var width = $( "#config-steps #width" ).val();
var height = $( "#config-steps #height" ).val();
var type = $( "#config-steps #type" ).val();
var color = $( "#config-steps #selected-color" ).val();
if ( width.match( /^\d+$/ ) && height.match( /^\d+$/ ) && type.length > 0 && color.length > 0 ) {
$( "#checkout-message" ).show();
// Change visible price
$( "#change-price" ).html( calculate_price().toFixed( 2 ) );
} else {
return false;
}
});
}
【问题讨论】:
-
尝试删除变量声明后的空格。
-
您的一个或多个变量未定义。你确定你的选择器是好的吗?
-
if( $( "#config" ) )在 jQuery 中总是如此! -
@epascarello 哦,好吧。那我怎样才能以正确的方式检查呢?
-
表示
#width或#height或两者均未找到或不是#config-steps的后代。
标签: javascript jquery regex match