简体   繁体   English

根据选择的选项更改最大长度

[英]change maxlength depending of the option selected

I've seen similar questions posted and tried to change them to meet my needs but I don't know enough about javascript to do it. 我看到过类似的问题,并试图更改它们以满足我的需求,但是我对javascript的了解还不够。 I need that when a user change the dropdown select, the "titre text field" maxlength is dynamically changed 我需要当用户更改下拉选择时,“ titre文本字段” maxlength会动态更改

a, bc and d max maxlength should be 40 and maxlength should be 2 a,bc和d max maxlength应该是40,maxlength应该是2

my code is below, I don't know why but it is not working correctly. 我的代码在下面,我不知道为什么,但是不能正常工作。 when i change the selection option to "E", the maxLength of the titre input box is the default value (40), not 2. This is the problem. 当我将选择选项更改为“ E”时,纤度输入框的maxLength是默认值(40),而不是2。这就是问题所在。 It should be 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> 

You have a questionmark at the end of the function definition. 你必须在函数定义的最后一个问号。

<!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM