没有 jquery 试试这个:
<script type="text/javascript">
function toggle(me, other) {
me.setAttribute('style', 'display: none');
var otherElement = document.getElementById(other);
otherElement.removeAttribute('style');
if(otherElement.tagName.toLowerCase() === 'select') {
myValue = me.getAttribute('value');
for(var n = 0; n < otherElement.options.length; n++) {
if(otherElement.options[n].getAttribute('value') == myValue) {
otherElement.options[n].select;
break;
}
}
}
else {
myValue = me.options[me.options.selectedIndex].getAttribute('value');
otherElement.value = myValue;
}
}
</script>
<input onclick="toggle(this, 'the_select');" id="the_input" />
<select onchange="toggle(this, 'the_input');" id="the_select" style="display:none">
<option value="male">male</option>
<option value="female">female</option>
<option value="alien">alien</option>
</select>
用 jquery 试试这个
<script type="text/javascript">
function initToggle(select, input) {
var s = jQuery('#' + select);
var i = jQuery('#' + input);
i.click(function(){
i.hide();
s.show().val(i.val());
});
s.change(function(){
s.hide();
i.show().val(s.val());
});
}
</script>
<input id="the_input" />
<select id="the_select" style="display:none">
<option value="male">male</option>
<option value="female">female</option>
<option value="alien">alien</option>
</select>
<script type="text/javascript">
jQuery(function(){
initToggle('the_select', 'the_input');
});
</script>
或使用 jquery 和动态
<script type="text/javascript">
function initToggle(input, options) {
var s = jQuery('<select />')
.hide();
for(var n = 0; n < options.length; n++) {
s.append(jQuery('<option/>')
.attr('option', options[n])
.html(options[n]));
}
var i = jQuery('#' + input).after(s);
var conf = function(a, b, method) {
a.unbind(method).bind(method, function(){
a.hide();
b.show().val(a.val());
});
}
conf(i, s, 'click');
conf(s, i, 'change');
}
</script>
<input id="the_input" />
<script type="text/javascript">
jQuery(function(){
initToggle('the_input', ['male', 'female', 'alien']);
});
</script>