admin管理员组

文章数量:1394099

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

And using 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 ?

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

And using 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 ?

Share Improve this question edited Jan 18, 2012 at 4:08 asked Jan 18, 2012 at 4:01 user1099407user1099407 4
  • What is the click event bound to? Isn't the output correct? – Andrew Whitaker Commented Jan 18, 2012 at 4:04
  • Doesn't it have to be a string? – tekknolagi Commented Jan 18, 2012 at 4:07
  • 2 Are you using view source to check if it changed? – James Montagne Commented Jan 18, 2012 at 4:08
  • You better not use attr to set value. – gdoron Commented Jan 18, 2012 at 4:18
Add a ment  | 

4 Answers 4

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

You mentioned Jquery so I am going to assume you are using it. 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

Use the val method:

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

Don't use jquery only half the way. And don't use attr function to set a value.

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

本文标签: jqueryUsing Javascript to change a value in a textboxStack Overflow