2012-09-11 28 views
0

为什么不能正常工作?Jquery append当从下拉列表中点击选项

 <script> 
    $(document).ready(function(){ 
    $('#custom_field option').click(function(){ 
     $('#custom_field_input').append('<tr><td></td></tr>');  
     $('#custom_field_input td:last').append($(this).find(":selected").text()); 
    }); 
    }); 
    </script> 

我发现有此一.change功能和它的作品,但我需要有文本附加即使在选择下拉列表的值没有变化并不涉及我。

含义。

用户单击选项1,附加选项1文本。

用户再次点击选项1,另一个选项1文本被附加。

+0

你需要“在选项1用户点击,附加选项1文本。 用户点击选项1再次,另一个选择1文本被追加。” ?? – Gautam3164

+0

类似于http://stackoverflow.com/questions/898463/fire-event-each-time-a-dropdownlist-item-is-se-with-jquery –

+0

只是明白为什么**。改变**是好的但**。点击**不是 – Lawrence

回答

0

充分利用custon_field_input元素custom_field_opotion的点击为空的,下面的代码:

<script> 
$(document).ready(function(){ 
$('#custom_field option').click(function(){ 
    $('#custom_field_input').html('');  
    $('#custom_field_input').append('<tr><td></td></tr>');  
    $('#custom_field_input td:last').append($(this).find(":selected").text()); 
}); 
}); 
</script> 
+0

还是不能这样做 – Lawrence

0
<script> 
$(document).ready(function(){ 
$('#custom_field').change(function(){ 
    $('#custom_field_input').append('<tr><td></td></tr>');  
    $('#custom_field_input td:last').append($(this).val()); 
}); 
}); 
</script> 

这应该在你的情况下工作,以及

+0

没有。 .change方法不起作用,因为每次点击都需要它 – Lawrence

0

尝试

<script> 
    $(document).ready(function(){ 
    $('#custom_field option').click(function(){ 
    $('#custom_field_input').html('');  
    $('#custom_field_input').append('<tr><td></td></tr>');  
    $('#custom_field_input td:last').append($(this).find(":selected").text()); 
}); 
}); 
</script> 
0

您可以使用模糊或聚焦事件而不是点击。

$('#custom_field option').focusout(function(){ 
.... 
}); 

$('#custom_field option').blur(function(){ 
.... 
}); 
0

试试这个。

jQuery(function() { 
    jQuery('#custom_field').click(function() { 
     jQuery("#custom_field_input").val(jQuery("#custom_field_input").val() + jQuery("option:selected", this).text()); 
    }); 
}); 
0

试试这个:做了2个改动。使用的live incase您的选择将在以后动态填充。如果不使用简单的.click()。而不是.find(“:selected”),使用.find(“option:selected”)。

<script> 
$(document).ready(function(){ 
$('#custom_field option').live('click',function(){ 
    $('#custom_field_input').append('<tr><td></td></tr>');  
    $('#custom_field_input td:last').append($(this).find("option:selected").text()); 
}); 
}); 
</script> 
相关问题