得到变量&放入其他输入

时间:2015-08-18 15:51:59

标签: jquery

这是我的剧本:

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:poleRatingsAveragepoleRatingsUserspoleRatingsScore。我试过这个:

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

我的剧本中出错了什么?

2 个答案:

答案 0 :(得分: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);

})

非常非常非常感谢您的帮助!

答案 1 :(得分:0)

这将返回元素id

的值
<script>
    function handleChange(cb) {
        if(cb.checked == true) {
           document.getElementById("check21").checked = true;
           alert('eyaicecold');
        } else {
           alert('Message 2');
           var x = document.getElementById("check21").disabled= false;
        }
    }
</script>

这里你试图在id的末尾获得与“-value”具有相似id的其他元素

alert(poleRatingsAverage); 

所以你缺少的是

// 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您正在寻找具有特定值的ID