首页 > 解决方案 > 如何自动提交带有文本框 onchange 值的表单?

问题描述

<html>
<head></head>
<body>
    <div class="col">
        <form action="#" method="post">
            <select id="periodname" class="drpdwn" value=" " onchange='myFunction()'>
                <?php foreach($period as $period): ?> 
                    <option><?= $period['period']; ?></option>
                <?php endforeach; ?>
            </select>
        </form>
    </div>
    <div class="col" style="width:200px">
        <form action="myprofile.php" method="post" name='periodGet' id='periodGet'>
            <input name='periodText' autocomplete="off" value="Period">
            <input type="submit" value="Go">
        </form>
    </div>
</body>
</html>

这是我整个系统的一部分,我问如果文本框值发生更改而不按提交按钮或输入按钮,如何自动提交表单谢谢

标签: javascriptphphtml

解决方案


您不需要任何函数来触发选择值的 onchange()。You can simply submit the form when the select value is changed using this.form.submit()in onchange()function as an example below.

<select id="periodname" class="drpdwn" value=" " onchange='this.form.submit()'>

$("#textF").keyup(function() {
    console.log($("#textF").val());
    $('#form2').delay(200).submit();
});

$("#textF2").change(function() {
    console.log($("#textF2").val());
    $('#form2').delay(200).submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="path1">
    <select id="periodname" class="drpdwn" value=" " onchange='this.form.submit()'>
        <option>test1 </option>
        <option>test2 </option>
        <option>test3 </option>
    </select>
</form>




<form action="path2" id="form2">
    <input type="text" id="textF" placeholder="onkeyup()">
    <input type="text" id="textF2" placeholder="onchange()">
</form>


推荐阅读