I need to remove some values from a hidden and text input box using jQuery, but somehow this is not working
Example:
<input type="hidden" value="abc" name="ht1" id="ht1" />
<input type="text" name="t1" id="t1" />
I use the following jQuery code to remove the values with an onclick event
$('#rt1').click(function() {
$('#t1').val();
$('#ht1').val();
});
Can I empty the contents of the input box and开发者_C百科 clear the value of the hidden field using jQuery?
You should do this:
$('#rt1').click(function() {
$('#t1').val('');
$('#ht1').val('');
});
That is, pass an empty string. Either that, or use removeAttr (query.removeAttr('value')
).
$('#rt1').click(function() {
$('#t1').attr('value', '');
$('#ht1').attr('value', '');
});
Shorter version
$('#rt1').click(function() {
$('#t1, #ht1').val('');
});
You just need to pass an empty string in the val()
function or, you can use the more generic attr()
function that sets a given attribute to a given value:
$('#rt1').click(function() {
$('#t1').attr("value", "");
$('#ht1').attr("value", "");
});
This should be:
$('#rt1').click(function() {
$('#t1').val('');
$('#ht1').val('');
});
When val() function doesn't have parameter it will serves as getter not setter
$(document).ready(function(){
$('input').click(function(){
$(this).removeAttr('value');
});
});
//remove value when click
//without effect input type submit
$(document).ready(function(){
$('input:not(:submit)').click(function(){
$(this).removeAttr('value');
});
});
精彩评论