HTML选择POSTing文本而不是<option> selected </option>的值

时间:2015-03-06 23:26:42

标签: php jquery html forms

出于某种原因,我通过POST提交的表单是发送选项文本而不是表单的选项值。这是我的选择声明:

<select name = "newfreq">
     <option val='1'>every week</option>
     <option val='2'>every 2 weeks</option>
</select>

当我在PHP中打印出$ _POST数组时。我有:

 [newfreq] => every 2 weeks

我对jquery特别陌生,我用它以模态方式呈现表单,所以我在下面粘贴完整的表单/ div和javascript,以防它相关:

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>


  <script>
  $(function() {
    var dialog, form,



    dialog = $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,

    });

     form = dialog.find( "form" ).on( "submit", function( event ) {
     // event.preventDefault();

    });

    $( "#create-user" ).button().on( "click", function() {
      dialog.dialog( "open" );
    });
  });
  </script>


<input id = "create-user" type="submit" tabindex="-1" value = "Change deal" >

<div id="dialog-form" >


  <form method = "post" action = "details.php">
    <fieldset>
      <label for="name">Number of hours</label>
      <input type="text" name="newhours" id="name" value = <?php echo $totalhours; ?>  class="text ui-widget-content ui-corner-all">
      <label for="email">Frequency</label>
      <select name = "newfreq">
           <option val='1'><?php echo return_frequency($typedeal ,1)?></option>
            <option val='2'><?php echo  return_frequency($typedeal ,2); ?></option>
      </select>

      <input type = "hidden" name = "included" value = '<?php echo $included;?>'/>
      <input type = "hidden" name = "id" value = '<?php echo $id_bus;?>'/>
      <br>
      <input type="submit" tabindex="-1" >
    </fieldset>
  </form>
</div>

return_frequency()是一个php函数,每两周返回一次&#34;&#34;或&#34;每周&#34;取决于输入。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

<option val='1'>应为<option value='1'>

我知道可能Jquery让你很困惑,因为他们使用.val很多,但实际的HTML使用value,正常的Javascript使用.value也是如此。

我认为既然你没有正确指定一个值,浏览器必须以某种方式将文本放在value中。但是,不要依赖于此,因为我认为这可能是一个错误,并非所有浏览器都会这样做。

答案 1 :(得分:0)

更改为:

<select name = "newfreq">
 <option value='1'>every week</option>
 <option value='2'>every 2 weeks</option>
</select>