Rob*_*ert 3 javascript regex jquery match
使用这段jQuery,我检查一些字段是否匹配或不为空,但我收到此错误.
未捕获的TypeError:无法读取未定义的属性"匹配"
谁能告诉我这里做错了什么?
if ( width.match( /^\d+$/ ) && height.match( /^\d+$/ ) && type.length > 0 && color.length > 0 ) {
Run Code Online (Sandbox Code Playgroud)
这是完整的代码:
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;
}
});
}
Run Code Online (Sandbox Code Playgroud)
if( $( "#config" ) ) {
Run Code Online (Sandbox Code Playgroud)
jQuery总是如此,因为jQuery返回一个对象,对象是真实的.检查应检查将返回数字的长度,零为假.
if ($("#config").length) {
Run Code Online (Sandbox Code Playgroud)
现在当jQuery找不到元素时,val()将返回undefined,因此它找不到元素.