1
我在JSP中有7個文本字段。在我的JavaScript經常我需要集體填補這些文本框,空,驗證等javascript中統一遍歷元素?
所以目前在我的JS我保持自己的ID爲parameter_1,parameter_2等因爲它可以幫助我穿越在一起輕鬆使用id屬性。
JS:
function emptyThresholdTextFields(){
$('[id^="parameter_"]').each(function(i, value) {
$(this).val('');
});
}
function fillThresholdTextFields(data){
$("[name='minNumberOc']").val(data.minNumberOc);
$("[name='minDurationOc']").val(data.minDurationOc)
$("[name='maxNumberIc']").val(data.maxNumberIc)
$("[name='maxDurationIc']").val(data.maxDurationIc)
$("[name='maxNumberCellId']").val(data.maxNumberCellId)
$("[name='distinctBnumberRatio']").val(data.distinctBnumberRatio)
$("[name='minPercentDistinctBnumber']").val(data.minPercentDistinctBnumber)
}
JSP:
<s:textfield id="thresholdParameter_1"
label="Minimum Number of OG Calls" required="true"
name="minNumberOc"
onkeypress="return isNumber(event,'thresholdParameter_1')">
</s:textfield>
- 這是一個很好的做法,以保持ID的喜歡這種形式參數1,參數等?
- 如在
fillThresholdtextFields
填寫我必須訪問每個參數關聯。所以稍後如果參數增加,它將變得更加麻煩。是否有其他的替代方法,使1和2都可以輕鬆實現?
不錯的答案,+1 –