0
私は、ユーザーが最初に特定のページにアクセスしたときに値を入力できる入力フィールドをいくつか持っています。これらの値はデータベースに保存され、テキストボックスに値を入力します。合計値から5つの値の割合が表示されます。ラベルとテキストボックスの割合を計算する
ユーザーがフォームから離れて戻ると、テキストボックスの代わりにラベルが表示されます。次のコードは、ユーザーが次回戻ったときにラベルが作成されるため、正しいパラメータ ".text"または ".val"を動的に選択するようなタスクの正しい方向ですか?私は、ラベル値を自分で追加するのではなく、このプロセスを自動化したいと思います。
私はトラブル
function calcABC(e) {
//Declare array of numeric selectors
var arr = ["a", "b", "c", "d", "e"];
var x = "";
var y = "";
if (e == "label") {
x = "lbl_"
y = ".text()";
}
else {
x = "txt_"
y = ".val()";
}
//HAVING TROUBLE HERE VVVVVVV
alert(eval("$('#' + x + arr[0]) + y)"));
alert("hello");
//loop through the array and get the value for that array index
var before = 0;
for (var i in arr)
{
before += isNaN(parseInt($("#"+ x + arr[i]).val())) ? 0 : parseInt($("#" + arr[i]).val())
}
//Apply the total to the global variable
ABCTotal = before;
//Loop through the numbers and get the precentage value and append the value to the relevant area
for (var j in arr)
{
console.log($("#" + x + arr[i]).val())
var val = isNaN(parseInt($("#" + x + arr[j]).val())) ? 0 : parseInt($("#" + arr[j]).val());
$("#txt" + arr[j] + "Prec > small").text(calcPrec(val).toFixed(2) + "%");
}
//Set the total field
$("#txt_ABCTotal").text(ABCTotal);
}
// Nice function to quickly calculate the precent value from a total
function calcPrec(val)
{
return (val * 100)/ABCTotal;
}
を持っていますどこ私はそれが理にかなって願っていますマークしています!とにかく助けられた助け!あなたが尋ねた
$('#' + x + arr[0])[y]()
: – MisterJ
jqueryで読み取るパラメータを解析する際に問題が発生しているコードを更新しました。 (具体的には警告のもの) – imBlue
evalの使用を避けるためには、 'y = 'text''または' y =' val''を実行し、 '$( '#' + x + arr [0] )[y]() ' あなたの具体的な問題は何ですか?どのようなエラーがありますか? – MisterJ