gpt4 book ai didi

php - 如何将输入框更改为单击时选择

转载 作者:行者123 更新时间:2023-11-30 10:36:52 27 4
gpt4 key购买 nike

我有一个包含值“男性”的输入框。我怎样才能改变那个输入框进入选择将包含两个选项“男性”和“女性”onclick。任何帮助将不胜感激

<?php

echo '<input type="text" name="gender" id="gender" value="male" onclick="changeSelect();"/>';

?>

html页面------------------------

<script type="text/javascript" > 

function changeSelect(){


}


</script>

最佳答案

没有 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>

关于php - 如何将输入框更改为单击时选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13509001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com