Javascript 当我在
我有一个代码,在下面的选项中列出。当我选择“其他”时,组合框应转换为文本。我试着用onchange函数编写一些脚本 功能充电器{ ifele.value==其他{ 我在另一个里面; document.getElementByIdinputtype.innerHTML=; } } 为每个标记添加值属性 选择onchange,检查select的值 如果是另一种情况,在这种情况下,使用替换儿童 [[编辑]根据OP的评论] 您可以将此消息传递给函数,以确定更改了哪个元素,并且为了代码的可重用性,可以将一个代码传递给所有类似的行为。 我们需要应用于新输入的id取自函数定义中的this关键字。 在每个select标记中,添加以下属性,就像我在下面的代码中所做的那样 在函数定义中,使用Javascript 当我在,javascript,jquery,Javascript,Jquery,我有一个代码,在下面的选项中列出。当我选择“其他”时,组合框应转换为文本。我试着用onchange函数编写一些脚本 功能充电器{ ifele.value==其他{ 我在另一个里面; document.getElementByIdinputtype.innerHTML=; } } 为每个标记添加值属性 选择onchange,检查select的值 如果是另一种情况,在这种情况下,使用替换儿童 [[编辑]根据OP的评论] 您可以将此消息传递给函数,以确定更改了哪个元素,并且为了代码的可重用
function check(elm){ // elm holds the changed select tag
elm.value // will return the value of currently changed element
elm.getAttribute('id') // will return the id of the current attribute, which we use later to apply to input
请参见下面的代码
函数校验{
ifelm.value==s5{
var inp=document.createElement'input';
设置属性'type','text';
inp.setAttribute'id',elm.getAttribute'id';
setAttribute'name','inp';
elm.parentNode.replaceChildinp,elm;
}
}
对
不
好啊
不好
另外
对
不
好啊
不好
另外
对
不
好啊
不好
另外
或者类似于jQuery: $'inp'.changefunction{ 如果此值为'S5'{ $this.replacetwith } } 对 不 好啊 不好 另外 试一试
到目前为止你试过什么?给我们看一些javascript。到目前为止你都尝试了什么?数据应该怎么处理?它会被提交吗?。。。?请添加更多的上下文。您可以在这里查看datalist@A.Lau是的,它真的很有用:谢谢。您认为即使不将value属性设置为option,它也会检查值吗?谢谢。它起作用了!但若我有3套以上的选择在一行中,若用户选择其他任何一个,它应该进入文本框。但在这种情况下,我只能得到一个。请稍等。我一到办公室就会给出解决办法。现在我在路上。你能分享一下你的想法吗idea@Jsel,谢谢你等我的朋友。我更新了代码。你现在可以查看了。非常感谢!让我试试这个
function check(elm){ // elm holds the changed select tag
elm.value // will return the value of currently changed element
elm.getAttribute('id') // will return the id of the current attribute, which we use later to apply to input
<select name="inp" id="inp" >
<option value="S1">YES</option>
<option value="S2">NO</option>
<option value="S3">OK</option>
<option value="S4">NOT OK</option>
<option value="S5">Other</option>
</select>
<input id='box' type="text" />
$( "#inp" )
.change(function () {
$( "#inp option:selected" ).each(function() {
if($( this ).text() == "Other"){
$("#box").show();
$('#inp').hide()
}else{
$("#box").hide();
}
});
})
.change();
1. Instead of creating a new tag element
2. we can just replace the existing select tag to input tag
3. Pros of using this method will reduce the script.
Refer this [plunker][1] for further information.
[1]: https://plnkr.co/edit/2ZztfpmvAhJZMQGtgZPw?p=preview