why don't work this js code? after click:
outputform #find_sc
is "true".
output .pagination a
is "undefined" .
"there is no" output in form select option
(In this case the alert is not displayed).
$('.pagination a, form #find_sc, form select option').live('click', function(e) {
e.preventDefault();
if($('.pagination a')){
var dirc = $(this).attr('href'); // this is a link tag <a></a>
}
if($('form #find_sc')){
var dirc = $(this).attr('class'); // this is a button tag <button></button>
}
if($('form select option')){
var dirc = $(this).attr('class'); // this is a selectbox. i want after click on option in it, was active and attr class in selectbox. <select><option></option></select>
}
alert(dirc);
$.get( dirc, function(html) {
//$('#ok').append(html);
$('#num_count').replaceWith( $(html).find('#num_count') );
$('tr#paginate').replaceWith( $(html).find('tr#paginate') );
$('.pagination').replaceWith( $(html).find('.pagination') );
});
return false;
});
What is your solutions o开发者_JS百科r suggestions?
EDIT: please click on following images in my example
form #find_sc
--is this-->
.pagination a
--is this-->
form select option
--is this--> -> [i want after click on option in select, was active and attr class in selectbox.] EXAMPLE: my problem in here
You can't know which of the jQuery selectors "triggered" the event.
You know only the active element, which is $(this)
so just take either its href
attribute, if exists, or otherwise take its class
attribute:
$('.pagination a, form #find_sc, form select option').live('click', function(e) {
e.preventDefault();
var sender = $(this);
var dirc = sender.attr("href") || sender.attr("class");
alert(dirc);
...
});
Edit: to catch drop down list change event, you can't use the click
instead of change
. To prevent duplicating your code, put it into a named function:
function MyEventHandler(e) {
e.preventDefault();
var sender = $(this);
var dirc = sender.attr("href") || sender.attr("class");
alert(dirc);
...
}
Then attach the proper events:
$('form select').live('change', MyEventHandler);
$('.pagination a, form #find_sc').live('click', MyEventHandler);
The problem that I saw are the conditions given inside the if
statements
For example, $('.pagination a')
will always be true even if it points to no elements. This is a JQuery object due to which it is a truthy value.
Similarly for other if
conditions.
Replace the if conditions with the check for length
property. That is, replace $('.pagination a')
with $('.pagination a').length
length
returns the number of elements pointed by this variable
You should use the length
property to check whether jQuery
returned any elements or not. Also in the get
success handler you can cache $(html)
into a local variable and use it at multiple places. Try this
$('.pagination a, form #find_sc, form select option').live('click', function(e) {
e.preventDefault();
if($('.pagination a').length > 0){
var dirc = $(this).attr('href'); // this is a link tag <a></a>
}
if($('form #find_sc').length > 0){
var dirc = $(this).attr('class'); // this is a button tag <button></button>
}
if($('form select option').length > 0){
var dirc = $(this).attr('class'); // this is a selectbox. i want after click on option in it, was active and attr class in selectbox. <select><option></option></select>
}
alert(dirc);
$.get( dirc, function(html) {
var $html = $(html);
$('#num_count').replaceWith( $html.find('#num_count') );
$('tr#paginate').replaceWith( $html.find('tr#paginate') );
$('.pagination').replaceWith( $html.find('.pagination') );
});
return false;
});
精彩评论