我只需要在一些文本的末尾添加一个星号,这是基于用户从下拉框中选择的。如果用户从下拉列表中选择选项一,则附加星号,如果用户选择第二个选项,则删除星号。我想我需要用jquery来做这件事,但是我在弄清楚我应该使用哪个函数时遇到了麻烦。我试图将星号包装在div中,并使用Jquery切换div的css显示属性,但这看起来非常笨拙。有没有更好的方法来动态添加/删除文本?
发布于 2010-04-21 00:17:27
<select>
<option value="1">First option</option>
<option value="2">Second option</option>
</select>
Some text <span id="asterisk" style="display:none">*</span>
$(document).ready(function() {
// bind to the select's change event
$("select").change(function() {
if($(this).val() == "2") {
$("#asterisk").hide();
} else {
$("#asterisk").show();
}
// fire the event once when the page loads
// so the visible state of the asterisk is based on the selected option
}).change();
});https://stackoverflow.com/questions/2676715
复制相似问题