问题
I have something weird that is happening and I was hoping someone can shed some light on it for me. I am using the qtip2 plugin on an element on the page. Here is the code for it:
$('.tooltipEdit').click(function(){
var id = $(this).attr('id');
$(this).qtip({
show:{
event: 'click',
solo: true
},
content: {
text:'<form id="tooltipForm"><input type="text" value="'+$(this).text()+'" /> <button id="tooltipSave">Save</button></form>'
},
position : {
my : 'top center',
at: 'bottom center',
target: $('#'+id+'')
},
hide: {
delay : 500,
fixed: true
},
style: {
classes: 'ui-tooltip-blue ui-tooltip-shadow ui-tooltip-rounded'
},
events: {
show: function(event, api) {
$('#tooltipSave').click(function()
{
var contact = 0;
if($('#'+id+'').attr('id') == 'callFromContactName')
{
contact = $('#contactFromId').val();
}
if($('#'+id+'').attr('id') == 'callToContactName')
{
contact = $('#contactToId').val();
}
$.ajax(
{
type: "GET",
url: "php/myurl.php",
dataType: 'json',
data: 'callid='+$('#callid').val()+'&field='+$('#'+id+'').attr('id')+'&value='+encodeURIComponent($('#tooltipForm input').val())+'&contact='+contact,
success: function(j)
{
return true;
},
error: function()
{
return false;
}
});
return false;
});
}
}
});
As you can see I am creating a form within the tooltip with a "save button to submit back to the server.
I have seen two things that are strange.
1) When I click on the element with that class, the first click does nothing but then the second time I click on it the tooltip shows up. Why is it not coming up on the first click?
2) There are times when the wrong text is in showing inside the form within the tooltip. If I am using a dynamic population of the form, why would it not be the correct text all the time?
I have read about using jquery's destroy() method for pages with multiple tooltips and I bet that would help but I do not know where to use it.
All help would be appreciated!
UPDATE: You cna see the code in this jsFiddle: http://jsfiddle.net/DULDx/1/ Thanks!
回答1:
You should be applying the qtip plugin in an each statement. Change the click to each. Also to fix the form data you need to reference this before the qtip call. Prior to using the plugin on your element you need to get a reference to this
otherwise the qtip event when you use this
will be referencing something different.
Demo: http://jsfiddle.net/lucuma/PqyFj/1/
$('.tooltipEdit').each(function(){
var id = $(this).attr('id');
var $this = $(this); // get a reference so we can use it in the show event of qtip
$(this).qtip({
show:{
event: 'click',
solo: true
},
content: {
text:'<form id="tooltipForm"><input type="text" value="'+$this.text()+'" /> <button id="tooltipSave">Save</button></form>' // we use $this now to reference the element that was outside qtip
},
position : {
my : 'top center',
at: 'bottom center',
target: $('#'+id+'')
},
hide: {
delay : 500,
fixed: true
},
style: {
classes: 'ui-tooltip-blue ui-tooltip-shadow ui-tooltip-rounded'
},
events: {
show: function(event, api) {
$('#tooltipSave').click(function()
{
var contact = 0;
if($('#'+id+'').attr('id') == 'callFromContactName')
{
contact = $('#contactFromId').val();
}
if($('#'+id+'').attr('id') == 'callToContactName')
{
contact = $('#contactToId').val();
}
$.ajax(
{
type: "GET",
url: "php/myurl.php",
dataType: 'json',
data: 'callid='+$('#callid').val()+'&field='+$('#'+id+'').attr('id')+'&value='+encodeURIComponent($('#tooltipForm input').val())+'&contact='+contact,
success: function(j)
{
return true;
},
error: function()
{
return false;
}
});
return false;
});
}
}
});
来源:https://stackoverflow.com/questions/10915266/qtip2-click-event-not-working-on-first-click