我正在处理一项代码,该代码可以让我按不同的“按钮”以在HTML表单的文本类型输入的内部插入一个值。
目前,我能够使代码与一个按钮连接到事件侦听器一起修改文本字段中的值它可以工作。
我已经尝试了围绕每个按钮的单个事件侦听器,并在主函数中添加一个参数字段,但是,由于某种原因,我仍然无法工作。
这是HTML和JS的最小可重现代码段:
const textField = document.querySelector('input[name = "textField"]');
var myElement = document.forms['Test']['textField'];
const validTextStrings = ["Hello World", "Goodbye World"]
var button = document.querySelector('input[name = "button"]');
//var button2 = document.querySelector('input[name = "button2"]');
button.addEventListener("click", updateTextField);
function updateTextField() {
console.log("button pressed");
console.log(button.value);
if (allValidValues(button.value)) {
myElement.setAttribute('value', button.value);
}
}
function allValidValues(value) {
for (i = 0; i < validTextStrings.length; i++) {
if (value == validTextStrings[i]) {
return true;
}
}
console.log("Invalid value");
return false;
}
<body>
<form name="Test">
<input type="button" name="button" value="Goodbye World" class=".btn">
<input type="button" name="button2" value="Hello World" class=".btn">
<input type="text" value='TEST' placeholder="TEXT NEEDS UPDATING" name="textField">
</form>
</body>
I am working on a piece of code that would allow me to press different "buttons" to insert a value inside of a text type input of a HTML form.
Currently, I am able to get the code working with one button connected to an event listener to modify the value in the text field, however, when I tried to add multiple buttons to edit the same text field, for some reason, I cannot get it to work.
I've tried different work arounds such as to add individual event listeners to each button and add a parameter field to the main function, however, I still can't get it to work for some reason.
Here's the minimum reproducible code snippets for the HTML and JS:
const textField = document.querySelector('input[name = "textField"]');
var myElement = document.forms['Test']['textField'];
const validTextStrings = ["Hello World", "Goodbye World"]
var button = document.querySelector('input[name = "button"]');
//var button2 = document.querySelector('input[name = "button2"]');
button.addEventListener("click", updateTextField);
function updateTextField() {
console.log("button pressed");
console.log(button.value);
if (allValidValues(button.value)) {
myElement.setAttribute('value', button.value);
}
}
function allValidValues(value) {
for (i = 0; i < validTextStrings.length; i++) {
if (value == validTextStrings[i]) {
return true;
}
}
console.log("Invalid value");
return false;
}
<body>
<form name="Test">
<input type="button" name="button" value="Goodbye World" class=".btn">
<input type="button" name="button2" value="Hello World" class=".btn">
<input type="text" value='TEST' placeholder="TEXT NEEDS UPDATING" name="textField">
</form>
</body>
发布评论
评论(3)
你快到了!只需使用包含目标值的事件参数即可。
解决方案
每个具有一个单个参数:基于 event 。
建议
我建议您使用ID,而不是课堂。使用ID可以更容易定位特定元素。我所做的另一个更改是为按钮创建单独的EventListeners。
您可以在“侦听器函数参数”上获得事件对象,以使您可以使用目标值。事件对象还具有其他属性 。
You are almost there! Just use the event parameter that contains the value of the target.
Solution
Every EventListenerCallback has a single param: an object based on Event.
Suggestion
I suggest you to use id than that of class. Using ids it is easier to target a specific element. Another change I did was to create separate eventListeners for the buttons.
You get an event object on the listener function parameter which gives you target value to use. The event object also has other properties to look into.
我选择了
document.queryselectorall(“。btn”);
的所有按钮
document.queryselectorall(“。btn”);
代码>均匀。我还将
()=&gt; {}
updateTeTextfeild()
函数绑定。在您的代码
QuerySelector()
仅选择第一个元素,因此它仅选择第一个按钮
。因此,每当您要选择一个以上的元素时,我们都必须使用
queryselectorall
或getElementsByClassName
。querySelector
仅适用于选择特定元素,例如选择id> id> id
attribute
的元素。最后,我不确定,但切勿通过。“```(点运算符)
class> class属性。喜欢使用
class =“ btn”而不是
class =“。btn”`````I have selected all the buttons by
document.querySelectorAll(".btn");
Then I have
loop
through all this buttons and attachedclick
even. I have also bind the()=>{}
updateTextFeild()
function inside of the loop.In your code
querySelector()
will only select the first element so it was selecting onlyfirst button
.So, whenever you want to select more than one element we must use
querySelectorAll
orgetElementsByClassName
.querySelector
is only good for selecting specific element like selecting element byid
attribute
. Lastly, I am not sure but never pass.```` (dot operator) inside of
classattribute. Like use
class="btn"instead of
class=".btn"```.这是您必须使用事件委托的典型情况
this is a typical case where you have to use event delegation