Get two values from one with jQuery
<div>345000000, 46400000000</div>
How do I make two values from the text of this开发者_如何学JAVA <div>
? They are divided by comma.
We should get var value_1 = 345000000
and var value_2 = 46400000000
<div id="numbers">345000000, 46400000000</div>
<script>
$(document).ready(function(){
var parts = $("#numbers").html().split(",");
var value1 = parseInt(parts[0], 10);
var value2 = parseInt(parts[1], 10);
alert(value1);
alert(value2);
});
</script>
Hope this helps. Cheers
Look up Javascript's string.split() function.
Grab the value, split 'em up.
<div id="nums">12, 15</div>
var str=document.getElementById("nums").innerHTML;
var nums = str.split(",");
alert(nums[0]); //gets first number
Answer: http://jsfiddle.net/morrison/F38FC/
Notes:
- Shows how to grab multiple divs.
- Uses JavaScript's
String.split()
. - Open the console to view the pieces.
精彩评论