javascript序列化复选框值
我在这个项目中有一个表单,我正在序列化它,以便我可以将它传递给PHP,并根据输入值进行一些数据库调用。一切正常,直到我在表单中添加了一个复选框并尝试序列化它javascript序列化复选框值,javascript,serialization,checkbox,checked,Javascript,Serialization,Checkbox,Checked,我在这个项目中有一个表单,我正在序列化它,以便我可以将它传递给PHP,并根据输入值进行一些数据库调用。一切正常,直到我在表单中添加了一个复选框并尝试序列化它 <input type="checkbox" name="reduceHolOT" id="reduceHolOT" checked="checked" ></input> 与我表单中的其他数据一起?您应该为复选框添加值属性。你知道,这是个好习惯 但是如果选中复选框,serialize应该将字符串“reduceHo
<input type="checkbox" name="reduceHolOT" id="reduceHolOT" checked="checked" ></input>
与我表单中的其他数据一起?您应该为复选框添加值属性。你知道,这是个好习惯
但是如果选中复选框,serialize应该将字符串“reduceHolOT=on”放入结果中。如果未选中,则无论您是否有值,都不会显示任何内容。也许您应该尝试以下操作:
<!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>
序列化演示
主体,选择{
字体大小:12px;
}
形式{
保证金:5px;
}
p{
颜色:红色;
保证金:5px;
字体大小:14px;
}
b{
颜色:蓝色;
}
单身
单身2
倍数
倍数2
倍数3
支票1
支票2
无线电1
无线电2
函数showValues(){
var str=$(“form”).serialize();
$(“#结果”).text(str);
}
$(“输入[type='checkbox'],输入[type='radio']”)。在(“单击”,显示值);
$(“选择”)。打开(“更改”,显示值);
showValues();
你想做什么?为什么不把表单提交给php呢?我的网页上有20个不同的事件,javascript将序列化所有内容,并根据失去焦点或点击的内容将其发布到不同的php文件中。我正在使用ajax。每个php文件使用序列化的不同部分,最后一个文件使用复选框值进行数据库调用。哇,那个网站太棒了!!谢谢那么它会给我一个开或关的值吗?它仍然没有真正起作用,因为如果你取消选中复选框,然后尝试运行它,那么警报是空白的。您可以通过在带有value=“0”的复选框之前添加同名的隐藏标记来解决此问题。如果选中该复选框,它将覆盖隐藏值。如果不是的话,你会得到0。@Stephen你知道,这肯定会奏效,但我刚刚习惯了有条件地测试这些东西。是的,我意识到这就是它的工作原理,并在php中放入了If语句,因为If['reduceHolOT']没有设置。然后我手动设置变量,你确定吗。你用过同样的吗?在发布之前先自己检查一下
<!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>