2

我在这个项目中有一个表单,我正在序列化,以便我可以将它传递给 PHP 并根据输入值进行一些数据库调用。一切正常,直到我在表单中添加了一个复选框并尝试对其进行序列化。

<input type="checkbox" name="reduceHolOT" id="reduceHolOT" checked="checked" ></input>

如何将此复选框的选中或值属性放入

$('form').serialize()

连同我表格中的其他数据?

4

2 回答 2

1

你应该为你的复选框添加一个值属性。你知道的好习惯。

但是如果选中复选框,则序列化应该将字符串“reduceHolOT=on”放入结果中。如果未选中,则无论您是否有值,都不会显示任何内容。

于 2011-08-11T17:46:49.293 回答
0

Maybe you should try this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>serialize demo</title>
  <style>
  body, select {
    font-size: 12px;
  }
  form {
    margin: 5px;
  }
  p {
    color: red;
    margin: 5px;
    font-size: 14px;
  }
  b {
    color: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<form>
  <select name="single">
    <option>Single</option>
    <option>Single2</option>
  </select>

  <br>
  <select name="multiple" multiple="multiple">
    <option selected="selected">Multiple</option>
    <option>Multiple2</option>
    <option selected="selected">Multiple3</option>
  </select>

  <br>
  <input type="checkbox" name="check" value="check1" id="ch1">
  <label for="ch1">check1</label>
  <input type="checkbox" name="check" value="check2" checked="checked" id="ch2">
  <label for="ch2">check2</label>

  <br>
  <input type="radio" name="radio" value="radio1" checked="checked" id="r1">
  <label for="r1">radio1</label>
  <input type="radio" name="radio" value="radio2" id="r2">
  <label for="r2">radio2</label>
</form>

<p><tt id="results"></tt></p>

<script>
  function showValues() {
    var str = $( "form" ).serialize();
    $( "#results" ).text( str );
  }
  $( "input[type='checkbox'], input[type='radio']" ).on( "click", showValues );
  $( "select" ).on( "change", showValues );
  showValues();
</script>

</body>
</html>

Source Link

于 2015-04-21T04:11:17.927 回答