2015-08-18 170 views
0

裏面這是我的腳本:獲取變量和其他放輸入

jQuery('#postcustomstuff tbody > tr:nth-child(3)').click(function() { 

    // Get IDs 
    var poleRatingsAverage = jQuery('#postcustomstuff tbody > tr:nth-child(2)').attr('id'); 
    var poleRatingsUsers = jQuery('#postcustomstuff tbody > tr:nth-child(4)').attr('id'); 
    var poleRatingsScore = jQuery('#postcustomstuff tbody > tr:nth-child(3)').attr('id'); 

    var ratings_average = jQuery(poleRatingsUsersTextAreaID).val(); 
    var ratings_users = jQuery(poleRatingsUsersTextAreaID).val(); 
    var ratings_score = parseInt(ratings_average) * parseInt(ratings_users); 

}) 

我有一個問題,我的jQuery腳本。我必須從變量中獲得ID:poleRatingsAverage,poleRatingsUserspoleRatingsScore。我試過這個:

alert(poleRatingsAverage); <---- This return good but in next part I must add VALUE to this: 

// Values Start 
var ratings_average = jQuery(poleRatingsAverage+'-value').val(); 
var ratings_users = jQuery(poleRatingsUsers+'-value').val(); 
var ratings_score = parseInt(ratings_average) * parseInt(ratings_users); 

jQuery(poleRatingsScore+'-value').val(ratings_score); // Math 
// Values End 

我在做什麼錯在我的腳本?

+0

就像在所有查詢選擇器前放置'#'一樣簡單嗎?只需把'poleRatingsAverage =「#」+ poleRatingsAverage;'放在開頭。 – Archer

回答

0

這將返回元素的id值

alert(poleRatingsAverage); 

在這裏你試圖讓具有相似的ID與該ID的末端「值」

// Values Start 
var ratings_average = jQuery(poleRatingsAverage+'-value').val(); 
var ratings_users = jQuery(poleRatingsUsers+'-value').val(); 
var ratings_score = parseInt(ratings_average) * parseInt(ratings_users); 
jQuery(poleRatingsScore+'-value').val(ratings_score); // Math 
// Values End 

那麼其他元素你缺少的是

jQuery('#'+poleRatingsAverage+'-value').val(); 

你必須對jquery說,你正在尋找一個id與特定的值ue

+0

您不知道'poleRatingsAverage'前面是否有散列,因此您不應該發佈這是一個答案,因爲它可能是完全錯誤的。採取行動之前,您應該獲得更多信息。 – Archer

1

是的!其作品!

這是編輯後的腳本:

jQuery('#postcustomstuff tbody > tr:nth-child(3)').click(function() { 

    // Get IDs 
    var poleRatingsAverage = jQuery('#postcustomstuff tbody > tr:nth-child(2)').attr('id'); 
    var poleRatingsUsers = jQuery('#postcustomstuff tbody > tr:nth-child(4)').attr('id'); 
    var poleRatingsScore = jQuery('#postcustomstuff tbody > tr:nth-child(3)').attr('id'); 

    var ratings_average = jQuery('#'+poleRatingsAverage+'-value').val(); 
    var ratings_users = jQuery('#'+poleRatingsUsers+'-value').val(); 
    var ratings_score = parseInt(ratings_average) * parseInt(ratings_users); 

    jQuery('#'+poleRatingsScore+'-value').val(ratings_score); 

}) 

非常,非常,非常感謝您的幫助!

相關問題