我已经看过类似的问题并尝试更改它们以满足我的需求,但我对javascript知之甚少。 我需要当用户更改下拉选择时,“titre text field”maxlength动态更改
a,b c和d max maxlength应为40 和maxlength应为2
我的代码如下,我不知道为什么但是它无法正常工作。当我将选择选项更改为“E”时,滴定输入框的maxLength是默认值(40),而不是2.这就是问题所在。应该是2。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title page</title>
<script type="text/javascript">
function changeValue(dropdown) {
var option = dropdown.options[dropdown.selectedIndex].value,
field = document.getElementById('titre');
if (option == 'a' || option == 'b' || option == 'c' || option == 'd') {
field.maxLength = 40;
} else if (option == 'e') {
field.value = field.value.substr(0, 2); // before reducing the maxlength, make sure it contains at most two characters; you could also reset the value altogether
field.maxLength = 2;
}
}?
</script>
</head>
<body>
<form action="converter.php" method="post">
<h2>Feel all field below:</h2>
<div>
Title: <input type="texte" name="titre" id="titre" maxLength="40"/> Format:
<select id="format" name="format" onchange="changeValue(this);">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<option value="d">D</option>
<option value="e">E</option>
</select>
</div>
<div>
<textarea name="texte" style="width: 415px; height: 155px;"></textarea>
</div>
<div>
<input type="submit" value="OK" />
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
您在函数定义的末尾有一个问号。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title page</title>
<script type="text/javascript">
function changeValue(dropdown) {
var option = dropdown.options[dropdown.selectedIndex].value,
field = document.getElementById('titre');
if (option == 'e') {
field.value = field.value.substr(0, 2); // before reducing the maxlength, make sure it contains at most two characters; you could also reset the value altogether
field.maxLength = 2;
} else {
field.maxLength = 40;
}
}
</script>
</head>
<body>
<form action="converter.php" method="post">
<h2>Feel all field below:</h2>
<div>
Title: <input type="texte" name="titre" id="titre" maxLength="40"/> Format:
<select id="format" name="format" onchange="changeValue(this);">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<option value="d">D</option>
<option value="e">E</option>
</select>
</div>
<div>
<textarea name="texte" style="width: 415px; height: 155px;"></textarea>
</div>
<div>
<input type="submit" value="OK" />
</div>
</form>
</body>
</html>