我有一个自动完成的ajax脚本,在字符输入字段时运行。我想在单击切换以允许手动完成时阻止此操作。我正在尝试使用.unbind,但它没有用?
function manualEntry() {
jQuery("#field_41_47").toggle();
if (jQuery('#field_41_47').css('display') == 'none') {
jQuery('#manual-company-entry').html('Enter company manually');
jQuery("#field_41_47").hide();
jQuery('#input_41_44').val('').change();
}
else {
jQuery('#manual-company-entry').html('Search for company');
jQuery("#field_41_47").show();
jQuery('#input_41_44').val(1).change();
xhr.unbind();
}
}
答案 0 :(得分:0)
示例简单设置(这是基于jsfiddle ajax所以不要指望它在不改变$ .ajax函数的情况下工作)
HTML
<input type="button" id="completetoggle" value="toggle auto complete on/off">
<input type="text" name="textfield" id="textfield">
<div id="target" ><div>
JS
$(document).on('click', '#completetoggle', function(){
toggle= ((toggle === true) ? false : true);
});
$(document).on('keyup', '#textfield', function() {
if(toggle===true) {
text=$('#textfield').val();
var returned = ajaxtext(text);
returned.done(function(html) {
$('#target').html(html);
});
}
});
ajaxtext= function(text){
return $.ajax({
url: '/echo/html/',
type: 'post',
data: {html: text},
cache: false
});
};