【发布时间】:2016-04-09 17:42:59
【问题描述】:
我正在尝试提交有关任何复选框的更改事件的表单。在这里,我有许多被标签标记包围的复选框,但是每当发生更改事件时,serialize() 方法都会为表单参数返回一个空值,而不是输入的值。标签标签是否会影响 Ajax 的工作?我是 Ajax 的新手,所以可能只有一个小错误。任何帮助将不胜感激。
JavaScript
<script src ="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var form = $('#ajaxform');
$("#ajaxform input:checkbox").on('change',function () {
//alert(this.name+' '+this.value+' '+this.checked);
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: $(this).parent("form").serialize(),
success: function (data) {
var result=data;
$('#content').html(result);
}
});
return false;
});
</script>
HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax form Example</title>
</head>
<body>
<div id='content'>
</div>
<form id='ajaxform' name='ajaxform' action='./ajaxformexample' method='post'>
First Name:
<input type='text' id='firstname' name='firstname' size='30' required/>
<br/>Last Name:
<input type='text' id='lastname' name='lastname' size='30' required/>
<br/>Email:
<input type='email' id='emailid' name='emailid' size='30' required/>
<br/>Password:
<input type='password' id='pwd' name='pwd' size='30' required/>
<br/>checkbox:
<input type='checkbox' id='cb' name='cb' value='1' />
<br/>checkbox2:
<input type='checkbox' id='cb1' name='cb1' />
<br/>
<label class="checkbox">
<input type="checkbox" value="50-200" name="price">50-200</label>
<label class="checkbox">
<input type="checkbox" value="200-500" name="price">200-500</label>
<label class="checkbox">
<input type="checkbox" value="500-1000" name="price">500-1000</label>
<label class="checkbox">
<input type="checkbox" value="1000-1500" name="price">1000-1500</label>
<label class="checkbox">
<input type="checkbox" value="1500-3000" name="price">1500-3000</label>
<label class="checkbox">
<input type="checkbox" value="3000-5000" name="price">3000-5000</label>
<label class="checkbox">
<input type="checkbox" value="5000-10000" name="price">5000-10000</label>
<label class="checkbox">
<input type="checkbox" value="10000-50000" name="price">10000-50000</label>
</form>
</body>
</html>
在上面的代码中,当我删除标签元素时,它工作正常并且我得到了所有预期的参数值,但是当我使用标签时它返回 null 而不是参数值。
【问题讨论】:
-
在更改标签的类名后尝试,
class="checkbox_abc"或其他任何内容... -
仍然无法正常工作@ManjeetBarnala
-
我尝试删除类属性,但无法正常工作。所以我想这与标签有关。
-
试试
$("#ajaxform").on('change','input[type="checkbox"]', function () { -
这工作正常!我正在获取所有参数的输入值,但这会将我重定向到 servlet,并且我正在 servlet 上获取输出,我希望在 index.jsp 上输出。