Here is my code
var input_buttons = ["#one","#two","#three"];
var substr = input_buttons.split(',');
for(var i=0; i< substr.length; i++)
{
substr.attr('value', '');
}
Why doesn't this work?
Here is my code
var input_buttons = ["#one","#two","#three"];
var substr = input_buttons.split(',');
for(var i=0; i< substr.length; i++)
{
substr.attr('value', '');
}
Why doesn't this work?
Share Improve this question edited Aug 24, 2014 at 3:46 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked Mar 9, 2011 at 15:12 KeithKeith 26.5k36 gold badges98 silver badges129 bronze badges 1-
You need to tell us, what your code is it supposed to do, because that doesn't make any sense at all.
input_buttons
is an array and arrays doesn't have a methodsplit
. Strings do have a methodsplit
which returns an array and array's don't have a methodattr
either. – RoToRa Commented Mar 9, 2011 at 15:18
7 Answers
Reset to default 4Your first problem is calling split(',') on an array. However, if you just want to set the values of all those to a blank string you can do:
$('#one,#two,#three').val('');
If you want to set different values you'd need to loop through:
$('#one,#two,#three').each(function() {
// this == the HTML node (not a jQuery element)
this.value = someValue; // someValue would set outside
};
You already have an array, there is nothing to split, this only works on strings. You'd also have to pass the ID to jQuery before you can cal attr
. In this case val
is even better.
var input_buttons = ["#one","#two","#three"];
for(var i=input_buttons.length; i--;) {
$(input_buttons[i]).val('');
}
But shorter would be using the multiple selector:
$('#one, #two, #three').val('');
or if you already have the array, create a string by join
ing the IDs:
$(input_buttons.join(',')).val('');
I'm wondering why you are calling:
var substr = input_buttons.split(',');
By the nature of your input_buttons, you already have an array. All you should have to do is:
var input_buttons = ["#one","#two","#three"];
for(var i=0; i< substr.length; i++)
{
$(input_buttons[i]).attr('value', '');
}
var input_buttons = ["#one","#two","#three"];
$.each(input_buttons, function(idx, value) {
$(value).val('');
});
Or even better and shorter:
$('#one, #two, #three').val('');
You could also give those elements a mon class name and then use this:
$('.className').val('');
your array contains just the id but not the actual object try this
var input_buttons = ["#one","#two","#three"];
for(var i=0; i< input_buttons.length; i++)
{
$(input_buttons[i]).removeAttr('value');
}
input_buttons
is already an array - don't split
it.
To use .attr
you need it to be a jquery object, so call $(input_buttons[i]).attr
Try the following to remove an attribute:
var input_buttons = ["#one","#two","#three"];
for(var i=0; i< input_buttons.length; i++)
{
$(input_buttons[i]).removeAttr('value');
}
The reason your code does not work is in the overloading of jQuery functions. .attr('value', '')
evaluates to .attr('value')
, which returns the value of value
as opposed to setting it. The reason is that ''
evaluates to false
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745308515a4621859.html
评论列表(0条)