简体   繁体   English

获取变量并放入其他输入

[英]Get variable & put inside other input

This is my script: 这是我的脚本:

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);

})

I have one problem with my jQuery script. 我的jQuery脚本有一个问题。 I must get IDs from variables: poleRatingsAverage , poleRatingsUsers and poleRatingsScore . 我必须从以下变量获取ID: poleRatingsAveragepoleRatingsUserspoleRatingsScore I tried this: 我尝试了这个:

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

What am I doing wrong in my script? 我的脚本在做什么错?

Yes! 是! It's works! 其作品!

This is script after editing : 这是编辑后的脚本:

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);

})

Very, very, very THANKS for help ! 非常非常非常感谢您的帮助!

this returns value of elements id 这将返回元素id的值

alert(poleRatingsAverage); 

here you are trying to get other elements that have similar id with "-value" on the end of that id 在这里,您尝试获取其他具有相似ID且其ID末尾带有“ -value”的元素

// 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

So what you are missing is 所以你缺少的是

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

You must say to jquery that you are looking for an id with specific value 您必须对jquery说您正在寻找具有特定值的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM