jquery函数未定义 第一 第二 第三 $(函数(){ $('sel1选项')。在('click',函数(e)上{ 如果($(this).is(':selected')){ $('')。附于('#续1'); } }); });
为什么这个功能不起作用?它给出错误“未定义函数”。正确的属性名称是jquery函数未定义 第一 第二 第三 $(函数(){ $('sel1选项')。在('click',函数(e)上{ 如果($(this).is(':selected')){ $('')。附于('#续1'); } }); });,jquery,Jquery,为什么这个功能不起作用?它给出错误“未定义函数”。正确的属性名称是src: <html> <head><script scr="http://code.jquery.com/jquery-1.8.2.min.js" ></script> </head> <div id="cont1"> <select id="sel1" name="sel1" multiple> <option value="First"
src
:
<html>
<head><script scr="http://code.jquery.com/jquery-1.8.2.min.js" ></script> </head>
<div id="cont1">
<select id="sel1" name="sel1" multiple>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
</select>
</div>
<script>
$(function() {
$('#sel1 option').on('click', function(e){
if($(this).is(':selected')){
$('<input value="'+$(this).val()+'" />').appendTo('#cont1');
}
});
});
</script>
<html>
</body>
<html>
v---------更正属性名称
您还应该检查标记,因为正确的标记必须包含
标记和一对正确放置的
标记
另外,考虑使用<代码> >代码>元素,而不是<代码>单击<代码> > <代码> <代码>,因为它是“跨浏览器”。
您必须编写SRC,BTW $(文档)。准备好(函数){更好。 v-------------- correct the attribute name
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
$(文档).ready(函数(){
$('sel1选项')。在('click',函数(e)上{
如果($(this).is(':selected')){
$('')。附于('#续1');
}
});
});
第一
第二
第三
提示:格式化代码永远不会有坏处
此外,请检查以改进代码。scr、许多正文标签等
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js" ></script>
<script>
$(document).ready(function() {
$('#sel1 option').on('click', function(e){
if($(this).is(':selected')){
$('<input value="'+$(this).val()+'" />').appendTo('#cont1');
}
});
});
</script>
</head>
<body>
<div id="cont1">
<select id="sel1" name="sel1" multiple>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
</select>
</div>
</body>
</html>
$(函数(){
$('sel1选项')。在('click',函数(e)上{
如果($(this).is(':selected')){
$('')。附于('#续1');
}
});
});
第一
第二
第三
你没有开头的
标签。而且
上的scr
而不是src
,@NRohler是对的,因为它在fiddle中工作得很好可怕的代码格式,你正在结束一个没有开头的正文。修复你的格式,再仔细看看。为什么document.ready
会“更好”.$(函数({})
只是一个速记,它做的事情完全相同,因为它更具可读性。你通过btwlol复制了他的html格式错误。真的,我得到了负1,因为我帮助了其他人。是的,约翰,它更好,因为它更具可读性。对不起,格式:)还有三个打开的html标记。@未定义是的,我跳过了:)很抱歉,伙计们写错了,很抱歉这个愚蠢的错误谢谢:)这是有用的,它工作很好,但如果我还想当用户取消选择或取消点击下拉菜单中的选项时,它会删除相应的文本框呢?@user1640566你好,你可以像我这样做
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function() {
$('#sel1 option').on('click', function(e){
if ($(this).is(':selected')) {
$('<input value="'+$(this).val()+'" />').appendTo('#cont1');
}
});
});
</script>
</head>
<body>
<div id="cont1">
<select id="sel1" name="sel1" multiple="multiple">
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
</select>
</div>
</body>
</html>