需要使用jquery按名称获取元素,为了获取名称值,它与samp (变量)连接在一起。无法形成名称元素。连接有一些问题,请帮助。
$("input[name^='resource_" + samp + "_']")
完整法典:
var samp = $(thisVal).attr('name'); //user defined name
$("input[name^='resource_" + samp + "_']").each(function(key,val){
alert("calcwk entered");
if ($(this).val() === '') {
theVal = 0;
}
else {
theVal = parseInt($(this).val());
}
tot = tot + theVal;
alert("calcwk exit");
});
发布于 2015-08-05 05:21:16
因为我们不能确定值"samp“所包含的格式,所以我们需要确保值被正确的引号所覆盖。
$('[property=value]');
工作,因为您没有空格,或者选择器无法立即知道属性值的结束位置,而
$('[property=my value]');
混淆了系统的解析器,因此需要正确地用引号“转义”或“包装”值,例如:
$('[property="my value"]');
这是我的你的代码的帮助版本
var samp = $(thisVal).attr('name'), //user defined name
tot = 0 //define the total
;
$('input[name^="resource_' + samp + '_"]').each(function(key,val){
var theVal = $(this).val(); // do a jQuery call once per item
if (theVal === '') {
theVal = 0; // if this is text change to 0
}
tot += parseInt(theVal); // no need with else, parseInt everything
alert("calcwk exit");
});
举个例子,我创建了这个JSFiddle:http://jsfiddle.net/fua9rtjd/
发布于 2015-08-05 05:00:02
name
是否具有所选的thisVal
元素的属性?这是为我工作的JsFiddle。
发布于 2015-08-05 04:47:11
尝试:
var samp = $(thisVal).attr('name'); //user defined name
$("input[name^=resource_"+samp+"_]").each(function(key,val){
alert("calcwk entered");
if( $(this).val() === '' ){
theVal = 0;
}
else{
theVal = parseInt($(this).val());
}
tot = tot + theVal;
alert("calcwk exit");
});
名称问题:'% name %‘-不需要使用’Right:$("input[name^=resource_"+samp+"_]")
‘
https://stackoverflow.com/questions/31832913
复制