我已经编写(并复制)了几行 Javascript,它很好地满足了我的目的。但我试图找出一种更好的方法(跨浏览器和更好的性能)来做到这一点。我从朋友那里复制了该isInteger
函数,但我不明白为什么我们在以下情况下检查字符串值:
if (((c < "0") || (c > "9"))) return false;
上述条件工作正常,但是当我将其更改为检查数值时,功能会中断。输入字段开始接受字母字符。这是我更改它时的样子:
if ((( c < 0 ) || ( c > 9 ) return false;
我试图注释掉部分,以便您了解正在发生的事情。此代码中是否还有任何安全漏洞?我读到 1innerHTML1 方法可以打开一些安全漏洞,因此我们需要对其执行“干净”操作。因此我选择使用 jQuery 的.html
方法(我是 JavaScript 新手)
有问题的页面:http ://thehotdeal.net/clients/wehtmlit/index.php?order/
$(document).ready(function() {
var total = 0;
function calcTotal() {
/* fetching some values from PHP variables and then performing calculations.
essentially this is multiplying number of pages by price per page
*/
/* <![CDATA[ */
var total_price_main_pages = ($("#pages").attr("value")) * (<?php echo $main_price; ?>),
total_price_sub_pages = ($("#subpages").attr("value")) * (<?php echo $sub_price; ?>);
/* ]] > */
$("input.calculate:checked").each(function() {
// This happens for each checked input field
// These are few additional otions available to the user. If selected then
// the price stored in their "data" attribute is added to the total
var value = $(this).attr("data");
total += parseInt(value);
});
total += (parseInt(total_price_main_pages)) + (parseInt(total_price_sub_pages));
$("#total").html("Total: <strong>" + total + "</strong>");
}
// This happens when the page loads
calcTotal();
$("input.calculate").click(function() {
total = 0;
calcTotal();
});
// function to check if an input is positive number(s). returns true if [ 0 <= s <= 9 ]
function isInteger(s) {
var i;
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}
// Checking the mainpage input (default value 1)
// (valid value is greater than or equal to 1 and less than 10)
$("#pages").keyup(function() {
var page = $(this).val();
// if user deletes the value in this input (blank)
// then just display a warning message and do nothing
if(page == ""){
this.value = "";
$("#pageError").html("Please enter a value equal or greater than 1.");
return false;
}
// if value is less than or equal to zero then
// then set 1 as the new value, remove the error message and call the calcTotal function
else if(page <= 0){
this.value =1;
$("#pageError").empty();
total = 0;
calcTotal();
}
// check if value is not a positive integer by calling the isInteger function
// if not a positive integer then set 1 as the new value,
//remove the error message and call the calcTotal function
else if(!isInteger(page)){
this.value =1;
$("#pageError").empty();
total = 0;
calcTotal();
}
// if value does not fall in any of the if statements i.e. value is acceptable
// remove the error message and call the calcTotal function
$("#pageError").empty();
total = 0;
calcTotal();
});
// check if value is not empty when user exits the input
// if empty then set value as 1, remove error message and call calcTotal function
$("#pages").blur(function() {
var page = $(this).val();
if(page == ""){
this.value = 1;
$("#pageError").empty();
total = 0;
calcTotal();
}
});
// Checking the subpage input (default value 0)
// (valid value is greater than or equal to 0 but less than 10)
$("#subpages").keyup(function() {
var page = $(this).val();
if(page == ""){
this.value = "";
return false;
} else if(!isInteger(page)){
this.value = 0;
total = 0;
calcTotal();
}
total = 0;
calcTotal();
});
$("#subpages").blur(function() {
var page = $(this).val();
if(page == ""){
this.value = 0;
total = 0;
calcTotal();
}
});
});