2010-12-27 8 views
1

ユーザーがフォームのdivを一度クリックすると、自動的にフォームを悲鳴をしたいです。JQueryフォーム自動提出サイドをdivをクリックして

私は以下を試みましたが、div内をクリックして送信しました。

オフ手
<div id="formDiv"> 
<form id="search"> 
    <input id="target" type="text" value="Field 1" /> 
    <input type="text" value="Field 2" /> 
</form> 
</div> 

// auto submit 
jQuery('#formDiv').live('blur',function() { 
    $(this).children('form').submit(); 

    }); 
+0

ユーザーが外をクリックしていない場合はどうdiv? – RobertPitt

+0

私の更新された回答をご覧ください。 –

答えて

1

、私はそれはそう、その後もそれを止めるために、DIVのクリックイベントにアタッチしe.preventDefault()、身体のクリックイベントにアタッチ提案することができる唯一のこと。あるいは、各フォーム要素のfocus/blurイベントに.delayでバインドすることもできます。 1つの要素からフォーカスを失って別の要素にフォーカスしていない場合は、フォームを送信します。

EDIT

これはあなたが探しているものであるかどうかを確認してください:

Javascriptを:

var queueSubmit; 

$('#sample input').each(function(i,e){ 
    $(this).bind('focus',function(){ 
     clearTimeout(queueSubmit); 
    }); 
    $(this).bind('blur',function(){ 
     queueSubmit = setTimeout(function(){ $('#sample').submit(); }, 1000); 
    }); 
}); 

$('#sample').bind('submit',function(){ 
    clearTimeout(queueSubmit); 
    alert('Pseudo-Submitted!'); 
    return false; 
}); 

サンプルHTML

<div id="main"> 
    <p>&nbsp;</p> 
    <div id="main-form"> 
     <form id="sample" method="POST" target=""> 
      <fieldset> 
       <legend>My Sample Form</legend> 
        First Name: <input type="text" id="first_name" /><br /> 
        Last Name: <input type="text" id="last_name" /><br /> 
        Telephone: <input type="text" id="telephone" /><br /> 
        <input type="submit" value="Submit" /> 
      </fieldset> 
     </form> 
    </div> 
    <p>&nbsp;</p> 
</div> 
イベントターゲット( event.target)は #formDivの子孫要素であるならば、私は体にクリックハンドラを入れて見てこれを行うだろう

Working Example Here

+0

フォーカスを使用しない場合、ユーザーがフォームを考慮してdivをクリックしてフォームを送信する最も単純な方法は何ですか? –

+0

残念ながら、それを行う良い方法はありません。ドキュメント全体でクリックイベントを捕捉し、フォームの不動産からクリックイベントを除外する必要があります。 –

1

$('#search').one('focus', function() { // the first time that a field in #search receives focus 
    var $formDiv = $('#formDiv'); 
    $(document.body).click(function(e) { // bind a click handler to the document body 
     if (!$formDiv.has(e.target).length) { // if the target element is not within formDiv 
      $('#search').submit(); // submit the form 
     } 
    }); 
}); 
関連する問題