这是html
<!DOCTYPE html>
<html>
<title>Ingredient</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="themes/receta.min.css" />
<link rel="stylesheet" href="css/jquery.mobile.structure-1.1.0.min.css" />
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.mobile-1.1.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/ingredients.js"></script>
<h1><center>INGREDIENTES</h1>
<ul>
<li onclick="this.parentNode.removeChild(this);">
<input type="hidden" name="ingredients[]" value="Cheese" />
Cheese
</li>
<li onclick="this.parentNode.removeChild(this);">
<input type="hidden" name="ingredients[]" value="Ham" />
Ham
</li>
</ul>
<select onchange="selectIngredient(this);">
<option value="Cheese">Cheese</option>
<option value="Olives">Olives</option>
<option value="Pepperoni">Pepperoni</option>
<option value="Milk">Milk</option>
</select>
</html>
这是jquery
function selectIngredient(select)
{
var $ul = $(select).prev('ul');
if ($ul.find('input[value=' + $(select).val() + ']').length == 0)
$ul.append('<li onclick="$(this).remove();">' +
'<input type="hidden" name="ingredients[]" value="' +
$(select).val() + '" /> ' +
$(select).find('option[selected]').text() + '</li>');
}
当我运行这个 html 和 jquery 时它不起作用。如果我选择它,它应该将另一种成分添加到列表中,但它不会发生。