2012-09-11 29 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> 
相關問題