我有struts表单,其中stuts textfield和普通html输入标签这里cssClass属性里面stuts textfield工作但类选择器不工作正常的html输入标签
<s:form id="Form" cssClass="form-horizontal" theme="css_xhtml" action="saveStudentMarks">
<div class="form-group">
<label class="col-xs-3 control-label">Class</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="className" id="studentclassInModal" readonly="readonly"/>
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Phone</label>
<div class="col-xs-3">
<s:textfield name="parentDTO.parentMobileNumber" cssClass="form-control marks" maxlength="10"/>
</div>
</div>
</s:form>
答案 0 :(得分:1)
删除循环,将html()
更改为val()
以设置文本输入的值
$(document).ready(function() {
$(".marks").on('input', function() {
calculateSum();
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
$(".marks").each(function() {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").val(sum.toFixed(2));
};
答案 1 :(得分:1)
尝试这样的事情:
$('#sum').focus(function(){ // It works on when textbox having id sum get focus
//var marks = [];
$('.marks').each(function(){
console.log($(this).val());
// push the marks in marks array and add them and show where you want
});
});
答案 2 :(得分:1)
做了一些改变,
使用.val()
代替.html()
$("#sum").val(sum.toFixed(2));
也计算百分比
$("#per").val((sum/100).toFixed(2));
答案 3 :(得分:0)
.each()
中的 jquery
用于逐个检查特定元素。
$(document).ready(function(){
$(".marks").keyup(function(){
calculateSum();
});
});
function calculateSum() {
var sum = 0;
$(".marks").each(function() {
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
console.log(sum)
}