动态创建时出错 [英] error while creating it dynamically

查看:128
本文介绍了动态创建时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var  purchas_probability = $( < label>< / label>
.prop( id,i)
.text(output [i] .count1 / 输出[I] .total_purchase);
.css({
width:' 40px'
height:' 40px'
});





i在count1中得到一些值,在标签中得到total_purchase它应该显示为1/3我在尝试这样做时会出错并且css也应该为它工作

解决方案

< label>< / label>
.prop( id,i)
.text(output [i] .count1 /输出[I] .total_purchase);
.css({
width:' 40px'
height:' 40px'
});





i在count1中得到一些值,在标签中得到total_purchase它应该显示为1/3我在尝试这样做时会出错并且css也应该为它工作


您收到此错误,因为您只是在两个变量之间放置字符串/。你需要连接它们:

 output [i] .count1 +   / + output [i] .total_purchase 



如果它仍然不起作用,那么你可能还没有附加元素。使用jQuery 追加 [ ^ ]或 appendTo [ ^ ]这样做。



另外,你需要在 text 方法后删除分号:

 ar purchas_probability = 


< blockquote>( < label>< / label>
.prop( id,i)
.text(output [i] .count1 / output [i] .total_purchase) // 这里不应该是分号
.css({
width:' 40px'
height:' 40px'
});

var purchas_probability=$("<label></label>")
                                                 .prop("id",i)
                                                 .text(output[i].count1"/"output[i].total_purchase);
                                                 .css({
                                                  width:'40px',
                                                  height:'40px'
                                                      });



i am getting some value in count1 and total_purchase in label it should display like this "1/3" i am getting error when i tried to do this and css should also work for it

解决方案

("<label></label>") .prop("id",i) .text(output[i].count1"/"output[i].total_purchase); .css({ width:'40px', height:'40px' });



i am getting some value in count1 and total_purchase in label it should display like this "1/3" i am getting error when i tried to do this and css should also work for it


You get this error because you just put the string "/" between the two variables. You need to concatenate them:

output[i].count1 + "/" + output[i].total_purchase


If it is still not working, then you probably have not yet appended the element. Use the jQuery append[^] or appendTo[^] to do that.

Also, you need to remove the semicolon after the text method:

ar purchas_probability=


("<label></label>") .prop("id",i) .text(output[i].count1"/"output[i].total_purchase) // here shouldn't be a semicolon .css({ width:'40px', height:'40px' });


这篇关于动态创建时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆