期待されるオブジェクト - jquery
-
03-07-2019 - |
質問
jqueryが原因で何らかの奇妙な理由で「オブジェクトが必要です」というエラーが発生し、これではフォームが「送信」されず、データベースにデータが入力されません。
jquery がなければ、データをデータベースに入力できます。しかし今はそうではありません。
私は主にasp.netコントロールを検証するためにjqueryを使用しました。
roosteronacid では、検証は完全に正常に機能しており、id プロパティも同じです。送信はサーバー側のコードを実行していないだけで、何が問題なのかわかりません。
編集:
これはjQueryコードです:
<script type="text/javascript">
$(document).ready(function() {
// add custom validation methods
$.validator.addMethod('phone', function(value, el, params) {
return this.optional(el) || /^[0-9,+,(), ,]{1,}(,[0-9]+){0,}$/.test(value);
}, 'Please enter a valid phone number');
$.validator.addMethod('numbers', function(value, el, params) {
return this.optional(el) || /^[0-9]+$/.test(value);
}, 'Invalid entry. Only Numeric is allowed.');
$.validator.addMethod('domainurl', function(value, el, params) {
return this.optional(el) || /^(http\:\/\/(?:www\.)?[a-zA-Z0-9]+(?:(?:\-|_)[a-zA-Z0-9]+)*(?:\.[a-zA-Z0-9]+(?:(?:\-|_)[a-zA-Z0-9]+)*)*\.[a-zA-Z]{2,4}(?:\/)?)$/.test(value);
}, 'Please enter a valid domain url');
$.validator.addMethod('selectone', function(value, element) {
return this.optional(element) || (value.indexOf("none") == -1);
}, 'Please select an option.');
$("#form1").validate({
debug: true,
rules: {
txt_name: {
required: true,
minlength: 2
},
txt_cmp: {
required: true,
minlength: 2
},
txt_tel1: {
phone: true,
required: true,
minlength: 3
},
txt_tel2: {
phone: true,
required: false,
minlength: 3
},
txt_mob: {
phone: true,
required: false,
minlength: 9
},
txt_email: {
required: true,
email: true
},
txt_domname: {
required: true,
domainurl: true
},
radiobt_domain: "required",
ddl_yremail: {
required: true,
selectone: true
},
ddl_email: {
required: true,
selectone: true
},
txt_space: {
required: true,
numbers: true
},
txt_calfr: {
required: true
},
txt_calto: {
required: true
}
},
messages: {
txt_name: {
required: "This field is required",
minLength: "Please enter a valid name"
},
txt_cmp: {
required: "This field is required",
minLength: "Please enter a valid commpany name"
},
txt_tel1: {
required: "This field is required",
minLength: "Please enter a valid telephone number"
},
txt_tel2: {
minLength: "Please enter a valid telephone number"
},
txt_mob: {
minLength: "Please enter a valid mobile number"
},
txt_email: {
email: "Please enter a valid email address",
required: "This field is required"
},
txt_domname: {
required: "This field is required"
},
radiobt_domain: "Select the Hosting Type"
}
});
});
</script>
コードに何か問題がありますか?
559行目でオブジェクトが予期されると表示されます。jquery.validate.js ファイルを確認したところ、次のコードが表示されています。
addWrapper: function(toToggle) {
if ( this.settings.wrapper )
toToggle = toToggle.add( toToggle.parents( this.settings.wrapper ) );
return toToggle;
}
jquery コードはすべてのエラーを適切な場所に表示しますが、修正されるとデータは送信されません。
私が使用しているプラグイン:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
解決
オブジェクトが定義されていない、参照されていない、またはスペルが間違っているオブジェクトにアクセスしようとすると、予期されるオブジェクトが発生します。どのオブジェクトが期待されているかを確認してください。Firefox ファイアバグを使用して JavaScript をデバッグするか、IE をオンにしてデバッグを実行して、ランタイムが見つけられないオブジェクトを取得します。
他のヒント
JQueryファイルを適切に参照することでこの問題を解決しました。サブディレクトリにありましたが、パスが正しくありませんでした。
ここで答えてください: 送信ボタンがサーバー側のコードをトリガーしない
「debug」は false に設定する必要があります。
私の推測では、jQuery 検証プラグインの使用にエラーがあると思われます。ASP.NET コントロールを 1 つだけ検証してみてください。これにより、エラーを見つけやすくなります。
$("#form1").validate({
rules: {
id_of_control_you_know_exists_in_the_rendered_html: {
required: true,
minlength: 2
}
}
});
別の可能性としては、ユーザー コントロールで ASP.NET コントロールを使用している可能性があります。この場合、レンダリングされた HTML 入力コントロールの id プロパティは、.aspx ページで設定したものとは異なります。
同じ問題がステージングサーバー上で発生しました。ファイルを比較すると、それらが同じであることがわかり、異なるサイトでまったく同じファイルをホストしても問題はなかったので、ファイルを置く特定のサイトでなければなりませんでした。トラブルシューティング後の原因は、IIS の Web サイトのプロパティで Footer.html ファイル設定を設定したため、サーバーがレンダリング時にスクリプト内にそれを挿入していたことでした。したがって、適切な準拠コードは破壊されてしまいます。IIS 設定のフッター プロパティをオフにしました - ビンゴ!
私もこの問題に直面しています。しかし、私の場合、私はjqueryバージョンの問題がありました。最新バージョンを入れたところ、IEでうまく動作しました。