-2

javascriptを使用してajax経由でフォームを送信するにはどうすればよいですか。これが私のコードです:

<script>
 function show_edit(bugid,device)
 {
   document.getElementById('updateform').style.visibility='visible';
   document.getElementById('U_bugid').value=bugid;
   document.getElementById('U_device').value=device;
 }
 function hide()
 {
   document.getElementById('updateform').style.visibility='hidden';
 }
</script>

ここに私の隠しフォームがあります

   <div id"update_form" >
  <form onsubmit="ajax_submit();">
  <fieldset>
  <label for="maskset">MASKSET</label><input type='text' name='bugid' id='U_bugid' readonly >
 <label for="device">DEVICE</label><input type='text' name='device' id='U_device'   readonly>

 <label for="reason">Comments</label><textarea rows=10 cols=40 name='reason'></textarea>
 <input id="S_update" class='S_update' value="Update Data" type="submit" >
</form>
</fieldset>
</div>

送信をクリックすると、新しいコメント(テキストエリアから)とともにajaxコードを介してすぐに送信されます。

  function show_edit(bugid,device)
  {
  var maskset;
  document.getElementById('updateform').style.visibility='visible';
  document.getElementById('U_bugid').value=bugid;
    document.getElementById('U_device').value=device;

   function ajax_submit()     
   {  //code in submitting ajax i already know; }
   }

これは機能しますか?

4

1 に答える 1

-5

あなたがしたいことはonsubmit="return ajax_submit()"、HTML に追加する必要があるコードの送信を停止し、関数が確実に返されるようにすることfalseです。

function ajax_submit()     
   {  
return false;
   }

コードの外観からすると、DOM 要素を選択するためにjQueryを使用すると、作業が大幅に簡素化されるため、大きなメリットが得られます。

その点で、jQuery Ajax を使用すると、あなたの人生は非常に簡単になります。特にjQuery AJAX functions を読んだ場合。

$.post("test.php", { bugid: $('#U_bugid').val(), device: $('#U_device').val() } );
于 2013-05-20T14:38:46.587 に答える