简体   繁体   English

如何通过选择标签更改占位符?

[英]How to change a placeholder by a select tag?

I want the placeholder of the text input to change depending on the option selected on the select tag.我希望文本输入的占位符根据在 select 标签上选择的选项而改变。 Also, I wanted to change the text input to disabled on the "DOC" option.另外,我想在“DOC”选项上将文本输入更改为禁用。

I gathered some code examples, but none of them were showing exactly what I was looking for, so I ended up with this code below.我收集了一些代码示例,但它们都没有显示出我正在寻找的内容,所以我最终得到了下面的代码。

 function changeDOC() { var x = document.getElementById("document").value; if (x == "doc") { document.getElementById('docu').disabled = true; } else if (x == "cpf") { document.getElementsById("doc")[0].placeholder = 'CPF'; } else if (x == "cnpj") { document.getElementsById("doc")[0].placeholder = 'CPF'; } }
 <select onchange="changeDOC" id="document"> <option value="doc" selected>DOC</option> <option value="cpf">CPF</option> <option value="cnpj">CNPJ</option> </select> <input type="text" class="form-control" id="docu" placeholder="Select a document.">

But it does nothing.但它什么也不做。

First, there is no such method as getElementsById() , it's getElementById() , which returns one element (if found).首先,没有像getElementsById()这样的方法,它是getElementById() ,它返回一个元素(如果找到)。 So there is no array returned from it, so passing an index isn't going to get you anything.所以没有从它返回数组,所以传递索引不会给你任何东西。

Next, your inline event attribute has to provide executable code that would invoke your function, but you didn't put parenthesis after the function name, so your code doesn't do this.接下来,您的内联事件属性必须提供可以调用您的函数的可执行代码,但您没有在函数名称后放置括号,因此您的代码不会这样做。 But, you should not be using inline HTML event attributes in the first place .但是, 您首先不应该使用内联 HTML 事件属性 Instead, do all your event binding in JavaScript with the standard addEventListener() method, which actually does take a callback method reference (no parenthesis).相反,使用标准的addEventListener()方法在 JavaScript 中完成所有事件绑定,该方法实际上采用回调方法引用(无括号)。

Now, it appears that you just want the input to have a placeholder that matches the selected item from the list, in which case, you don't need an if statement for that, just set the placeholder to the selected value directly.现在,您似乎只希望输入具有与列表中所选项目匹配的占位符,在这种情况下,您不需要if语句,只需将placeholder直接设置为所选值即可。 You only need the if to detect if the first item was selected and in that case, use the setAttribute method to set up the disabled functionality.您只需要if来检测是否选择了第一项,在这种情况下,请使用setAttribute方法来设置禁用的功能。

Also, since the select's first option is DOC and that choice should make the input disabled, you should add the disabled attribute to the input HTML statically from the start.此外,由于选择的第一个选项是DOC并且该选择应该禁用input ,因此您应该从一开始就静态地将disabled属性添加到input HTML 中。

See the comments below for more adjustments.有关更多调整,请参阅下面的评论。

 // Get references to elements you'll work with just once, not upon // each invocation of the event handler. Also, don't name or give things // id's that are also the name of an object (ie. document) let select = document.getElementById("list"); let input = document.getElementById("docu"); // Set up the event handler in JavaScript, not with HTML attributes like onchange select.addEventListener("change", changeDOC); function changeDOC() { input.placeholder = this.value; // `this` is a reference to the select itself if (this.value == "doc") { input.setAttribute("disabled", "disabled"); } else { input.removeAttribute("disabled"); } }
 <select id="list"> <option value="doc" selected>DOC</option> <option value="cpf">CPF</option> <option value="cnpj">CNPJ</option> </select> <input type="text" class="form-control" id="docu" placeholder="Select a document." disabled>

There are a few things wrong in your code, first off, there is no getElementsById() function in document , it's getElementById() since there can only ever be one element with a given ID in valid HTML (meaning you don't have to access the 0th element of the return value ( [0] )):您的代码中有一些错误,首先, document没有getElementsById()函数,它是getElementById()因为在有效的 HTML 中只能有一个具有给定 ID 的元素(这意味着您不必访问返回值的第 0 个元素 ( [0] )):

document.getElementsById("doc")[0].placeholder = 'CPF';

should be应该

document.getElementById("doc").placeholder = 'CPF';

Also you have to add parentheses to your markup to actually execute the function.此外,您必须在标记中添加括号才能实际执行该函数。

<select onchange="changeDOC" id="document">

Has to be必须

<select onchange="changeDOC()" id="document">

Also if you have DOC as selected by default, you should deactivate the element by default too, and activate it again if you choose the other options.此外,如果您默认选择了DOC ,您也应该默认禁用该元素,如果您选择其他选项,则再次激活它。

Here's the full code:这是完整的代码:

 function changeDOC() { var x = document.getElementById("document").value; if (x == "doc") { document.getElementById('docu').disabled = true; } else if (x == "cpf") { document.getElementById("docu").placeholder = 'CPF'; document.getElementById('docu').disabled = false; } else if (x == "cnpj") { document.getElementById("docu").placeholder = 'CPF'; document.getElementById('docu').disabled = false; } }
 <select onchange="changeDOC()" id="document"> <option value="doc" selected>DOC</option> <option value="cpf">CPF</option> <option value="cnpj">CNPJ</option> </select> <input type="text" class="form-control" id="docu" placeholder="Select a document." disabled="true">

There is a lot of syntax error in your code, just figure out errors but you should also go through over it.您的代码中有很多语法错误,只需找出错误​​即可,但您也应该仔细检查。

Number 1: You need to call the function not just place the name.第 1 点:您需要调用函数而不仅仅是放置名称。

<select onchange="changeDOC" id="document"> // Wrong
<select onchange="changeDOC()" id="document"> // Correct

Number 2: 2号:

document.getElementsById("doc") // Wrong
document.getElementById("doc") // Correct

Number 3: In the input tag you are using id="docu" and get with ('doc')数字 3:在输入标签中,您使用id="docu"并使用('doc')

 function changeDOC() { var x = document.getElementById("document").value; if (x == "doc") { document.getElementById('docu').disabled = true; document.getElementById("docu").placeholder = 'DOC'; } else if (x === "cpf") { document.getElementById('docu').disabled = false; document.getElementById("docu").placeholder = 'CPF'; } else if (x === "cnpj") { document.getElementById('docu').disabled = false; document.getElementById("docu").placeholder = 'CNPJ'; } }
 <select onchange="changeDOC()" id="document"> <option value="doc" selected>DOC</option> <option value="cpf">CPF</option> <option value="cnpj">CNPJ</option> </select> <input type="text" class="form-control" disabled id="docu" placeholder="Select a document.">

I have removed the extra id from the select option in HTML.我已经从 HTML 的选择选项中删除了额外的 id。

In JavaScript the select variable returns the whole select node with it's children.在 JavaScript 中, select 变量返回整个 select 节点及其子节点。

So I've added an event listener that on a change fires off a function with a parameter e that target's children (the one that's been selected) and add set's it's value as an attribute to the input tag.因此,我添加了一个事件侦听器,该侦听器在更改时会触发一个带有参数 e 的函数,该函数以目标的子代(已选择的那个)为目标,并将 set 的值作为属性添加到输入标签。

 const select = document.querySelector('select'); const input = document.querySelector('input'); select.addEventListener('change', e => { input.setAttribute('placeholder', `Select a ${e.target.value.toUpperCase()}`); })
 <select> <option value="doc" selected>DOC</option> <option value="cpf">CPF</option> <option value="cnpj">CNPJ</option> </select> <input type="text" class="form-control" id="docu" placeholder="Select a document." />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM