我在JavaScript中有一段代码,为什么它不起作用?我有一个文本框,一个列表项和一个按钮;当我点击按钮时,它应该运行函数cal()
。
cal()
是计算器,用于计算和返回值,如s1,k1,...
现在我在标签中显示了这个值,但代码没有运行!
<!DOCTYPE html>
<html>
<body>
<input type="text" id="txt" onkeyup="checkValue();" />
<input type="button" value="محاسبه" name="button3" onclick="cal();"/>
<label id="l1"></label>
<select id="mySelect">
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<script>
function checkValue() {
var t_value = document.getElementById('txt').value;
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect");
var j;
if (t_value > 5000000 & t_value <= 25000000) {
j = 5;
y.options.length = 4;
for (var i = 0; i <= 3 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 25000000 & t_value <= 50000000) {
j = 8;
y.options.length = 3;
for (var i = 0; i <= 2 ; i++) {
y[i].text = j;
j++;
}
}
}
function cal() {
var mv1 = 0, mq1 = 0, k1 = 0, p1 = 0, s = 0, x = 0, x2 = 0, p2 = 0, mv2 = 0, y = 0;
var x1 = 0;
var t_value = document.getElementById('txt').value;
var x = document.getElementById("mySelect").selectedIndex;
if (t_value > 5000000 & t_value <= 25000000) {
switch (x) {
case 6:
p1 = (0.05 * t1);
mv1 = t1 - p1;
x = mv1 / 250000;
x1 = Convert.ToInt32(x);
x2 = mv1 - (x1 * 250000);
if (x2 > 150000) {
y = x2 - 250000;
p2 = p1 - y;
mv2 = (y + x2) + mv1;
}
if (x2 < 150000) {
p2 = p1 + x2;
mv2 = mv1 - x2;
}
k1 = mv2 * 0.02;
mq1 = mv2 / (d1 - 1);
s = t1 + k1;
Label13.Visible = false;
Label19.Visible = false;
Label4 = document.getElementById(p2.ToString());
Label15.Text = mv2.ToString();
Label6.Text = mq1.ToString();
Label9.Text = k1.ToString();
Label10.Text = s1.ToString();
break;
}
}
}
</script>
</body>
</html>
请指导我实现目标。
我已经应用了下面建议的解决方案,但我的代码仍然失败。以下是更改后的结果代码:
function checkValue() {
var t_value = document.getElementById('txt').value;
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect");
var j;
if (t_value > 5000000 & t_value <= 25000000) {
j = 5;
y.options.length = 4;
for (var i = 0; i <= 3 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 25000000 & t_value <= 50000000) {
j = 8;
y.options.length = 3;
for (var i = 0; i <= 2 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 50000000 & t_value <= 100000000) {
j = 10;
y.options.length = 6;
for (var i = 0; i <= 5 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 100000000 & t_value <= 200000000) {
j = 10;
y.options.length = 11;
for (var i = 0; i <= 10 ; i++) {
y[i].text = j;
j++;
}
}
}
function cal() {
var mv1 = 0, mq1 = 0, k1 = 0, p1 = 0, s = 0, x = 0, x2 = 0, p2 = 0, mv2 = 0, y = 0;
var x1 = 0;
var t_value = document.getElementById('txt').value;
var x = document.getElementById("mySelect").selectedIndex;
if (t_value > 5000000 & t_value <= 25000000) {
switch (x) {
case 6:
p1 = (0.05 * t1);
mv1 = t1 - p1;
x = mv1 / 250000;
x1 = Convert.ToInt32(x);
x2 = mv1 - (x1 * 250000);
if (x2 > 150000) {
y = x2 - 250000;
p2 = p1 - y;
mv2 = (y + x2) + mv1;
}
if (x2 < 150000) {
p2 = p1 + x2;
mv2 = mv1 - x2;
}
k1 = mv2 * 0.02;
mq1 = mv2 / (d1 - 1);
s = t1 + k1;
document.getElementById("Label13").style.visibility = "hidden";
document.getElementById("Label19").style.visibility = "hidden";
document.getElementById("label15").innerHTML = mv2.toString();
document.getElementById("label4").innerHTML = p2.toString();
document.getElementById("label6").innerHTML = mq1.toString();
document.getElementById("label9").innerHTML = k1.toString();
document.getElementById("label8").innerHTML = s.toString();
break;
}
}
}
<select id="mySelect">
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
</select>
<input type="text" id="txt" onkeyup="checkValue();" /></div>
<input type="button" value="cal" name="button3" onclick="cal();"/>
<label id="Label15"></label>
<label id="Label4"></label>
<label id="Label6"></label>
<label id="Label9"></label>
<label id="Label19"></label>
答案 0 :(得分:2)
您正在将JavaScript代码与ASP.NET代码混合,以及导致问题的原因。这部分不正确:
Label13.Visible = false;
Label19.Visible = false;
Label4 = document.getElementById(p2.ToString());
Label15.Text = mv2.ToString();
Label6.Text = mq1.ToString();
Label9.Text = k1.ToString();
Label10.Text = s1.ToString();
独立于Label
变量(它们未在HTML或JS中定义),代码将失败(可能是静默的,但它会失败)。它看起来像ASP.NET代码,它肯定不会像JavaScript一样工作。
上面的一些错误:
ToString()
方法。它有一个toString()
(注意小写字母t)。要隐藏JavaScript中的元素,您必须执行以下操作:element.Visible = false;
。 .Visible
不是JavaScript。您可以通过将display
设置为none
或visibility
设置为hidden
来更改其样式来隐藏元素:
document.getElementById("Label13").style.display = "none";
或
document.getElementById("Label13").style.visibility = "hidden";
要在不需要element.Text = "my text";
的元素中设置文字,您可以通过更改innerHTML
来修改它:
document.getElementById("Label15").innerHTML = mv2.toString();
[注意在JS中确实不需要.toString()部分
发布新代码后更新。
新代码有一些问题会导致/失败:
Convert.ToInt32
来自.Net,它在JavaScript中不存在。您可以使用&#34;等效的&#34;在JS:parseInt
而不是。label15
,label4
,label6
应为{{1}分别是},Label15
和Label4
,依此类推其他变量); Label6
和d1
未定义,当代码到达时,它会失败并停止。您需要定义和分配它们(仅用于测试我为它们分配了值t1
)。t_value
和Label13
),当代码到达它们时,它将失败并停止。您应该评论或添加它们。 (如果您的代码只是一些实际包含它们的更大代码的示例,那么这可能不是一个真正的问题。)说完后,代码非常具体,只能使用小值组合(下拉列表必须为7,且数字必须为代码完全执行,在500到2500万之间。
我已在上面的代码中进行了指定的更改,您可以在此处看到它们:
Label8
&#13;
function checkValue() {
var t_value = document.getElementById('txt').value;
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect");
var j;
if (t_value > 5000000 & t_value <= 25000000) {
j = 5;
y.options.length = 4;
for (var i = 0; i <= 3 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 25000000 & t_value <= 50000000) {
j = 8;
y.options.length = 3;
for (var i = 0; i <= 2 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 50000000 & t_value <= 100000000) {
j = 10;
y.options.length = 6;
for (var i = 0; i <= 5 ; i++) {
y[i].text = j;
j++;
}
}
if (t_value > 100000000 & t_value <= 200000000) {
j = 10;
y.options.length = 11;
for (var i = 0; i <= 10 ; i++) {
y[i].text = j;
j++;
}
}
}
function cal() {
var mv1 = 0, mq1 = 0, k1 = 0, p1 = 0, s = 0, x = 0, x2 = 0, p2 = 0, mv2 = 0, y = 0;
var x1 = 0;
var t_value = document.getElementById('txt').value;
var x = document.getElementById("mySelect").selectedIndex;
// d1 and t1 were not defined, as a test, I assigned one value
var t1=t_value;
var d1=t_value;
if (t_value > 5000000 & t_value <= 25000000) {
switch (x) {
case 2:
p1 = (0.05 * t1);
mv1 = t1 - p1;
x = mv1 / 250000;
// replaced Convert.ToInt32 for parseInt
x1 = parseInt(x);
x2 = mv1 - (x1 * 250000);
if (x2 > 150000) {
y = x2 - 250000;
p2 = p1 - y;
mv2 = (y + x2) + mv1;
}
if (x2 < 150000) {
p2 = p1 + x2;
mv2 = mv1 - x2;
}
k1 = mv2 * 0.02;
mq1 = mv2 / (d1 - 1);
s = t1 + k1;
// commented the elements that don't exist, and fixed the names for the rest
//document.getElementById("Label13").style.visibility = "hidden";
document.getElementById("Label19").style.visibility = "hidden";
document.getElementById("Label15").innerHTML = mv2.toString();
document.getElementById("Label4").innerHTML = p2.toString();
document.getElementById("Label6").innerHTML = mq1.toString();
document.getElementById("Label9").innerHTML = k1.toString();
//document.getElementById("Label8").innerHTML = s.toString();
break;
}
}
}
&#13;
或者在这个JSFiddle上:http://jsfiddle.net/qvsxtyg4/