js と jquery を使用して小さな webproject のコードを書いています。その中で、ある時点でボタンをクリックすると、ダイアログが作成されます。ダイアログには、名前フィールドといくつかの数値フィールドを含むフォームがあります。ユーザー入力をチェックしてサーバーに送信することになっているほか、ブラウザのリストに名前フィールドを追加することで、親密なユーザーに、もう 1 つの項目が追加されました。2つの奇妙なことが起こっています -
1) フォームを投稿した後、送信ボタン ハンドラーのどこかに dialog('close') を発行しなくても、ダイアログ ボックスは自動的に閉じます。
2) 名前エントリはリストに追加されません。送信後にページ全体が更新されたかのようです。名前のリストの元のデフォルト エントリを使用します。
なぜこれが起こっているのか誰にも考えがありますか? あなたの援助のためにいくつかのコードを投稿します.代わりにAjaxを使用することを提案しないでください. これは、JS の方法に対する私の理解の根本的な欠陥を反映していると思います。他のテクノロジに切り替えるだけでなく、まずそれをクリアしたいと考えています。
<div id='dialog' title='Define New Matrix'>
<form name='form1' id='form1' method='post'>
<fieldset>
<label for="Name">Name</label>
<input type='text' name='nameofmatrix' id='Name' class='whitepanes'><br>
<label for="a11">a11</label>
<input type="text" name='a11' id='a11' class='whitepanes number-field'><br>
<label for="a22">a22</label>
<input type="text" name='a22' id='a22' class='whitepanes number-field'><br>
<button id='submit_button'>Submit</button>
<button id='cancel_button'>cancel</button>
</fieldset>
</form>
<p id='tip' style='color:red;'><i>All fields are required</i></p>
</div>
<script>
//#button_define is a button on whose clicking the dialog opens.
$('#button_define').click(function(){
$('#dialog').dialog('open');
$('#tip').html("<p style='color:red; font-size:small'>All fields are mandatory</p>");
});
$('#submit_button,#cancel_button').button();
$('#cancel_button').on('click',function(){
$('#dialog').dialog('close');
});
$('#submit_button').click(function(event){
event.preventDefault();
var name=$('input[name=nameofmatrix]').val();
//Validate is a function which returns a bool if validation proceeds correctly
var isCorrect = Validate();
if(isCorrect){
//if validated correctly then add to list
$('#form1').submit();
//$('#dialog').dialog('close');
$('#selectable').append("<li class='ui-widget-content'>",name,"</li>");
}
});
</script>