我已经看到发布的类似问题并试图更改它们以满足我的需求,但我对 javascript 的了解还不够。我需要当用户更改下拉选择时,动态更改“标题文本字段”最大长度
a、bc 和 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>