2013年9月2日星期一

jquery name selector return value array

Content of the page is this:




requirement is to get an array , containing all test * values.


My idea is this
I choose to use jquery selector to select the name starting with test input
now gained , but I want it to return directly input.value array , there is a good way to it ?
Here is what I wrote , but also new definition of array , as well as for recycling , too much trouble.
var test = $ ("input [name ^ = 'test']" ) ;
var arr = new Array ();
for (var i = 0; i arr [i] = test [i]. value;
}
PS: I tried using $ ("input [name ^ = 'test']"). val (); however , but only returns the first value .

But I think this is very troublesome , do not know if there was an easier way to do it ?
------ Solution ------------------- -------------------------
jquery seemingly no such
------ Solution ------- -------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery</title>
    <script language="javascript" type="text/javascript"
            src="Jscript/jquery-1.4.2.min.js"></script>
    <style type="text/css">
           body{font-size:12px;text-align:left}
           div{float:left;border:solid 1px #ccc;margin:8px;width:100px;height:65px;}
           span{float:left;border:solid 1px #ccc;margin:8px;width:45px;height:45px;background-color:#eee}
    </style>
    <script type="text/javascript">
function changeCSS(){
$("input[name*='test']").val("name包含'test'的").css("background-color","blue");
}


</script>
</head>
<body>
<input name="test1" value="a"/><br/><br/>
<input name="test2" value="b"/><br/><br/>
<input name="test3" value="c"/><br/><br/>

<p>
<button onclick="changeCSS();">test</button>&nbsp;&nbsp;&nbsp;&nbsp;

</body>
</html>




using jquery, very easy to achieve.

------ Solution ------------------------------------ --------
seems to mean very different with the landlord say ah
------ Solution --------------------- -----------------------
$ ("input [name * = 'test'] ") - this statement is to get these three input
------ Solution ------------- -------------------------------


What is input.value array
------ Solution ----------------------------- ---------------
meaning of the landlord is to get [a, b, c] instead of three input

------ Solution ------------------------------------ --------
jquery seemingly no such direct way , is estimated to write their own

------ Solution ------------------------------------ --------
can write

var arr = new Array();
var test = $("input[name^='test']").each(function(){
   arr.push($(this).val());
})


------ Solution ------------------------------------ --------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script language="JavaScript" src="/js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('input[name=t]').click(function(){
                             //获取的本来就是数组,你要获取数组的数值就需要遍历
$("input[name*='test']").each(function(){
alert($(this).val());
});
});

});

</script>
</head>
<body>

<input type="button" name="t" value="test">
<br>
<input name="test1" value="a"/>
<input name="test2" value="b"/>
<input name="test3" value="c"/>

</body>
</html>


------ For reference only ------------------------ ---------------

positive solution, I have not exactly explain how how it ..
I ask is the result : The easiest way to get an array , which is [a, b, c]
------ For reference only ---------- -----------------------------

sorry, you got it wrong .
- ---- For reference only ---------------------------------------
< br /> In this way the .
------ For reference only ---------------------------- -----------
engage in too much trouble to add these three input form tag on the outside, to get hold of this form this form id selector serialize () method to remove all value
------ For reference only ------------------------------------- -
this method no matter how many you have input are taken out of the ~

------ For reference only ---------------------------------- -----
look : http://blog.sina.com.cn/s/blog_5f66526e0101egse.html

没有评论:

发表评论