开发者

How to focusout good with an autocomplete click?

I have an input text and when it loses focus (focusout) the script POST (updates the text in the server) BUT, when 开发者_Python百科I click in the autocomplete options, the sent text is different, look:

<script>
$(document).ready( function() {
   var values = ["value1", "value2", "value3", "...", "value4"];
   $('input').bind( 'focusout', function() {
      $.post( '/add_value', $('form').serialize(), function(data) {} );
   });
   $('input').autocomplete({source:values} );
});
</script>
<form>
   <input type="text"/>
</form>

The user writes in the input: "va", then the autocomplete show something like this:

value1
value2
value3
value4

When the user click the autocomplete BOX the input loses focus, and send the value "va" to the server and not the clicked value.

The question is: How to control the click event to send the clicked value in the autocomplete and not the writer.


Add this:

var send_now = true;

$('input').bind( 'focusout', function() {
   if (send_now) {
          $.post( '/add_value', $('form').serialize(), function(data) {} );
      }
   });

$('.attribute_name').autocomplete( { source: values} );  

$('form').delegate( 'input', 'autocompleteopen', function() {                        
    send_now = false;
});

$('form').delegate( 'input', 'autocompleteclose', function() {
    $(this).focus();
    send_now = true;                    
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜