如果此问题不是重复问题,我将非常惊讶。我已经寻找了一段时间来回答这个问题,但我还没有找到答案。如果在我再次提出问题之前已经问过这个问题,如果你能指出我已经回答了这个问题的问题,那将是非常好的。jQuery更改所选表单的属性并提交表单
无论如何,我想使用jQuery提交表单,一旦它被更改。这部分工作得很好。工作不正常的部分是在表单更改时更改选择属性。目标很简单:每次表单被改变时,它被改变的值应该被赋予一个真正的选定attr,然后表单应该被提交。
的HTML
<!DOCTYPE html>
<html>
<head>
<title>Drop Down</title>
</head>
<body>
<form id='example' method='GET' action='index.php'>
<select id='menu' name='family'>
<option value='Kristen'>Kristen</option>
<option value='Henk'>Henk</option>
<option value='Andrew'>Andrew</option>
<option value='Derek'>Derek</option>
</select>
</form>
<script src='http://code.jquery.com/jquery-1.8.3.min.js'></script>
<script src='script.js'></script>
</body>
</html>
的JS
$(document).ready(function() {
$('#menu').change(function() {
//remove the select attr from the option that currently has a value of selected
$("option[selected='true']").removeAttr('selected');
//add the selected attr with value of true to the option that was clicked on
$(this).attr('selected', 'true');
//submit the form
this.form.submit();
});
});
当值改变,但所选择的ATTR是不会改变的形式被提交。我知道所选的attr没有变化,因为在表单提交后,第一个值Kristen总是显示为默认值。
在此先感谢您的答复,如果此问题最终成为重复问题,我将再次抱歉 - 同时我会继续寻找答案。
请参阅http:// jsfiddle.net/arunpjohny/nfR2r/1/ - 该属性的值似乎被重置为“selected”,而不是“true”,在铬版中 –