0

从下拉列表中选择一个选项时,我正在尝试提交表单。但它不起作用。谁能帮我解决这个问题。我在下面附上了我的代码。

提前致谢,

<div class="main" id="main">
  <h2>Sell / Rent Advertisments</h2>
  <form id="form1" name="form1" method="post" action="form.php">
  <table width="531" height="121" border="0">
    <tr>
      <td width="81"><div>
        <label>I wish to:</label>
      </div></td>
      <td width="76">
        <label for="mediaId"></label>
        <select name="mediaId" id="mediaId">


          <option selected value="Sale">Sale</option>
          <option value="Rent">Rent</option>
        </select>
      </td>
      <td width="360"><label for="type"></label>
        <select name="type" id="type">
        <option value="_Any" selected="selected">- - Not Specified - -</option>
<option value="1" onChange="document.forms['form1'].submit()">Apartment</option>
<option value="3" onChange="document.forms['form1'].submit()">Building</option>
<option value="4" onChange="document.forms['form1'].submit()">Hotels/Guest Houses</option>
<option value="5" onChange="document.forms['form1'].submit()">House</option>
<option value="6" onChange="document.forms['form1'].submit()">Land</option>
      </select></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
  </form>
</div>
4

4 回答 4

2

尝试

<select name="type" id="type"  onChange="document.forms['form1'].submit()">
    <option value="_Any" selected="selected">- - Not Specified - -</option>
    <option value="1">Apartment</option>
    <option value="3" >Building</option>
    <option value="4" >Hotels/Guest Houses</option>
    <option value="5" >House</option>
    <option value="6" >Land</option>
    </select>

也就是说,将onChange事件应用于select而不是option

于 2012-12-04T09:31:28.280 回答
1

HTML

<form id="form1" ...>
    <select name="type" id="type">
        <option value="_Any" selected="selected">- - Not Specified - -</option>
        <option value="1">Apartment</option>
        <option value="3">Building</option>
        <option value="4">Hotels/Guest Houses</option>
        <option value="5">House</option>
        <option value="6">Land</option>
    </select>
</form>

没有jquery,只有javascript

将 onchange 放在select标签中,而不是在每个单独的option标签中

<select name="type" id="type" onChange="document.forms['form1'].submit()">

jQuery

将功能与标记分开,并将此文本放入$.ready()

$('#type').change( function(){
    $('#form1').submit();
});
于 2012-12-04T09:36:52.463 回答
0

onChange 应该放在标签上,您“更改”选择的选项,但不更改选项上的任何内容。

<select name="type" id="type" onChange="document.forms['form1'].submit()">
于 2012-12-04T09:34:40.510 回答
-2

使用onClick而不是onChange喜欢

<option value="1" onclick="document.forms['form1'].submit()">Apartment</option>
于 2012-12-04T09:31:24.800 回答