1

JS

$('.add_to_list').live('keydown',function (e){
    if(e.keyCode == '13'){
        var holder=$(this).attr('hold'),
            val=$(this).val();
            if(holder == 'mf' ||holder == 'mp'){
                    var v='#'+holder;   
                    h='<li class="entery deletable"><input type="hidden" name="'+holder+'[]" value="'+val+'">'+val+'</li>';
                    $(v).append(h);
                    $(this).val('');
            }
        e.prevent_default();    
        return false;
    }
    $('#save_clinic').submit(function(){return false;});
    });

HTML

<form accept-charset="utf-8" method="post" id="save_clinic" action="#">
<p>
<b>Findings</b>
<ol id='mf'></ol>
<input type="text" hold="mf" class="add_to_list" value="" name="">
<!--the input hv no name cause i dont want it to be submitted, this is for adding only-->
</p>
<p>
<b>Medical Procedures:</b>
<ol id=mp></ol>
<input type="text" hold="mp" class="add_to_list" value="" name="">
<!--the input hv no name cause i dont want it to be submitted, this is for adding only-->
</p>
<input type=submit>
</form>

Problem:
I want to prevent submit on keypress ENTER and only allow submit on submit button click, but my current js prevent submit for both and if I remove

$('#save_clinic').submit(function(){return false;});

from js then the form auto submit when user is trying to populate the form.

Can some one please tell me what is the problem here? example

4

3 回答 3

1

The function is called preventDefault (no underscore, capital 'D'), not prevent_default. Give that a try and remove this line:

$('#save_clinic').submit(function(){return false;});
于 2012-12-27T07:59:04.273 回答
0

I think this should be outside:

$('.add_to_list').live('keydown',function (e){
   if(e.keyCode == '13'){
    var holder=$(this).attr('hold'),
        val=$(this).val();
        if(holder == 'mf' ||holder == 'mp'){
                var v='#'+holder;   
                h='<li class="entery deletable"><input type="hidden" name="'+holder+'[]" value="'+val+'">'+val+'</li>';
                $(v).append(h);
                $(this).val('');
        }
   }
   e.prevent_default(); // take this out from the if   
   $('#save_clinic').submit(function(){return false;});
});
于 2012-12-27T07:52:39.937 回答
0
$('.add_to_list').live('keydown',function (e){
        if(e.keyCode == '13'){
            var holder=$(this).attr('hold'),
                val=$(this).val();
                if(holder == 'mf' ||holder == 'mp' ||holder == 'orders'){
                        var v='#'+holder;   
                        h='<li class="entery deletable"><input type="hidden" name="'+holder+'[]" value="'+val+'">'+val+'</li>';
                        $(v).append(h);
                        $(this).val('');
                }
            //return false;
            e.preventDefault(); 

AND http://www.bloggingdeveloper.com/post/Disable-Form-Submit-on-Enter-Key-Press.aspx

both works fine now.. thanks all

于 2012-12-27T08:15:56.553 回答