简体   繁体   English

如何使用更新按钮使用JavaScript函数更改输入字段的文本?

[英]How to use update button to change the text of an input field using a JavaScript function?

I am doing an assignment to update a text field when an update button is pressed so far. 到目前为止,当我按下更新按钮时,我正在分配一个文本字段。 My code looks like this: 我的代码如下所示:

function updatePhotoInfo(){

var retrieve_images_script = SERVER + "/image.php?username="+USERNAME;
//@ToDo
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
imageData= JSON.parse(xhttp.responseText);
console.log(imageData.length); 
}
};
xhttp.open("GET", retrieve_images_script, false);
xhttp.send();

var dField = document.getElementsByClassName("dField").innerHTML;
var updateField = document.getElementsByClassName("dField").new.innerHTML;
console.log("dField");

if (dField != updateField) {
dField == updateField;
};

So generally what I would do is the following. 因此,通常我会做以下事情。 Say we have a button with ID=my-btn 假设我们有一个ID = my-btn的按钮

document.getElementsByID('my-btn').onclick = function(){
  //If the name of your text filed ID is my-txt
  document.getElementsByID('my-txt').innerHTML = "DONE!"

}

This is one way to do it, but if you have the option take a looking into jquery. 这是一种实现方法,但是如果您有选择的话,可以看看jquery。 It makes this kind of stuff a lot easier to manage. 它使这种东西易于管理。

See this Pen for details CODE PEN 有关详细信息,请参见此笔

EDIT: This one includes a field: SAME WITH Field 编辑:这包括一个字段: 与字段相同

暂无
暂无

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

相关问题 如何将按钮的值传递给输入文本字段,但使用javascript在文本字段中值应消失 - How to pass the value of button to input text field but value should be disappear in text field using javascript 如何在文本字段输入上执行javascript函数并用新值更新字段 - How can I execute a javascript function on text field input and update the field with new value 如何使用JavaScript函数将表单输入文本更改为大写 - How to change a form input text to uppercase using a JavaScript function 多个按钮将使用javascript中的一个功能将一个文本字段更改为该按钮的值 - Multiple buttons that will change the one text field to the value of that button using the one function in javascript 如何使用 javascript onclick 更改输入字段? - How to change input field using javascript onclick? 如何使用Javascript根据HTML中的输入文本字段更改文本元素? - How to change text element based on input text field in HTML using Javascript? 使用带有Javascript的两个选择菜单动态更新文本输入字段 - Update text input field dynamically using two select menus with Javascript 单击时使用javascript将某些文本更改为输入字段 - using javascript to change some text into an input field when clicked on 使用JavaScript更改输入文本字段的背景颜色 - Change background color of input text field using JavaScript 如何使用JavaScript更改点击时的按钮文字? - How to change button text on click using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM