简体   繁体   English

使用Javascript更改文本框中的值

[英]Using Javascript to change a value in a textbox

<input id="NameAjax" class="ac_input" type="text" value="">

And using jquery: 并使用jquery:

).click(function(e) { 
document.getElementById("NameAjax").value = 1;
}

But after the click the value does not change: 但点击后该值不会改变:

<input id="NameAjax" class="ac_input" type="text" value="">

I am looking for the output to look exactly like: 我正在寻找输出看起来完全像:

<input id="NameAjax" class="ac_input" type="text" value="1">

How to fix it ? 怎么解决?

$("#elementID").on('click', function() {
    $("#NameAjax").val('1');
});

You mentioned Jquery so I am going to assume you are using it. 你提到了Jquery,所以我假设你正在使用它。 If so try this: 如果是这样试试这个:

$('#NameAjax').attr('value','1')

The first part $('#NameAjax') selects the input and the second attr('value','1') sets the value attribute to 1 第一部分$('#NameAjax')选择输入,第二部分attr('value','1')将value属性设置为1

Use the val method: 使用val方法:

$('#NameAjax').val('1');

Don't use jquery only half the way. 不要只使用jquery的一半。 And don't use attr function to set a value. 并且不要使用attr函数来设置值。

$("element_idOrclass").click(function() { 
    $("#NameAjax").attr("value","1");
}

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

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