3

我有一个带有选择列表的小表单,每次选择项目时我都会提交表单。问题是,在选择一个值并提交表单后,所选项目不会保持选中状态。提交表单后,有什么方法可以在列表中保持选中的项目?(例如使用javascript)?

<form name="order_of_products_by_values" id="order_of_products_by_values" method="post" action="">
<select id="order_of_products_by_values" name="order_of_products_by_values"  onChange="this.form.submit();">   
<option value=1 >Pret crescator</option>
<option value=2 >Pret descrescator</option>
<option value=3 >Test</option>
<option value=4 >Test</option>
</select>
</form>

谢谢你!

4

2 回答 2

1

如果您不能使用服务器端解决方案,您可以在 onchange-Event 触发后设置一个 cookie 并提交表单。有关 javascript cookie 的信息,请查看以下站点:http ://www.quirksmode.org/js/cookies.html

于 2011-03-06T21:16:02.673 回答
0

如果你使用 AJAX 会怎样?如果您使用 jQuery,您可以提交表单,选择列表在选择后不会更改。

jQuery('#order_of_products_by_values').change(function() { 

    jQuery.post('ajax/test.php', jQuery("#order_of_products_by_values_FORM").serialize());

});

另外,请注意我更改了表单的 ID,因为它与可能导致冲突的选择列表的 ID 相同。

serialize 函数将序列化表单并将其发送到 test.php 脚本,在那里它可以像往常一样使用。前任:

$select = $_POST['order_of_products_by_values'];
于 2011-03-07T00:45:56.863 回答