开发者

Remove value of input using jQuery

开发者 https://www.devze.com 2022-12-08 00:03 出处:网络
I need to remove some values from a hidden and text input box using jQuery, but somehow this is not working

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');
  });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消