我有一个包含几个文本字段和下拉列表的表单。我能够捕获下拉列表更改的值。但是,当涉及到表单的单击时,该值不会得到更新。
$('#submit_form .required').change(function (e) {
var input = $(this);
alert(input.val()); ----> gives the dropdown value selected.
});
$('#submit_form').submit(function() {
$('#submit_form .required').filter(':visible').each(function () {
var input = $(this);
alert(input.val()); ----> it becomes null here.
});
});
知道为什么它在提交函数中提示null?
答案 0 :(得分:0)
基于您的代码的此示例在IE和Chrome中均可正常运行。你确定你没有任何其他代码搞乱选择的状态吗?
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<form id="submit_form" action="#">
<select id="test1" class="required">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
$('#submit_form .required').change(function (e) {
var input = $(this);
alert(input.val()); //----> gives the dropdown value selected.
});
$('#submit_form').submit(function() {
$('#submit_form .required').filter(':visible').each(function () {
var input = $(this);
alert(input.val()); //----> it becomes null here.
});
});
});
</script>