7

我试图没有提交按钮,当用户选择一个值时,表单是否可以提交?

<form method="post" action="<?php echo $_SERVER['PHP_SELF']  ?>" >

    <table class="form">

            <select name="category" class="formfield" id="category">
                <option value="-1"> Category </option>
                <?php
                    $sql_contry = "SELECT * FROM category";
                    $rs_c = mysql_query($sql_contry);
                    while ($row_c = mysql_fetch_array($rs_c)) {
                        echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>';  
                    }
                ?>
             </select>

    </table>

</form>
4

4 回答 4

28

这里有一个例子

<form>
    <select name='myfield' onchange='this.form.submit()'>
      <option selected>Milk</option>
      <option>Coffee</option>
      <option>Tea</option>
    </select>
    <noscript><input type="submit" value="Submit"></noscript>
</form>

使用 noscript 标签,它允许不支持 JavaScript 的浏览器仍然可以运行。

于 2013-04-26T19:57:56.510 回答
3

是的 - 使用 javascript:

html:

<form id="frm">
   <select onchange="onSelectChange();">
          <option>1</option>
           <option>1</option>
   <select>
</form>

js:

function onSelectChange(){
 document.getElementById('frm').submit();
}
于 2013-04-26T19:57:48.837 回答
1
<form action="product.php" method="POST">
<select onchange="this.form.submit();" name="prod">
    <option value="">Select product</option>
    <option value="1">abc</option>
    <option value="2">def</option>
    <option value="3">ghi</option>
    <option value="4">jkl</option>
    <option value="5">mno</option>
</select>

于 2019-02-20T11:58:52.640 回答
0

只需更改代码如下,我没有检查代码,所以可能会有一些错误,比如我不确定的大写/小写字母,它是 submit() 或 Submit() 等等..

<script language="javascript">
function submitForm(){
    var val = document.myform.category.value;
    if(val!=-1){
        document.myform.submit();
    }
}
</script>
<form method="post" name="myform" action="<?php echo $_SERVER['PHP_SELF']  ?>" >

    <table class="form">

            <select name="category" class="formfield" id="category" onchange="submitForm();">
                <option value="-1"> Category </option>
                <?php
                    $sql_contry = "SELECT * FROM category";
                    $rs_c = mysql_query($sql_contry);
                    while ($row_c = mysql_fetch_array($rs_c)) {
                        echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>';  
                    }
                ?>
             </select>

    </table>

</form>
于 2013-04-26T20:03:51.923 回答