Home>
When I select the button, I want to download it as CSV, but it doesn't work.
I think there is no problem with the value, but the contents of the downloaded file are not in CSV format.
Can you point out what is wrong?
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form>
<table>
<tr>
<td>A</td><td>B</td><td>C</td><td>D</td><td><input type = "checkbox" name = "11" value = " Apple, 1,200 "></td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td><td>d</td><td><input type = "checkbox" name = "11" value = " Melon, 2,4000 "></td>
</tr>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td><input type = "checkbox" name = "11" value = " Banana, 4,500 "></td>
</tr>
</table>
</form>
<p>CSV Expoert</p>
<script>
$(function () {
$('. btn'). on ('click', function () {
var text = '';
/ * Get value * /
$("[name = 11]: checked"). each (function () {
text = text + $(this) .val () + ',';
});
/ * Array * /
var array_data = [text];
/ * The following array will work * /
// var array_data = [['apple', 1,200], ['melon', 2,4000], ['banana', 4,500]];
/ * BOM ready * /
var bom = new Uint8Array ([0xEF, 0xBB, 0xBF]);
/ * Prepare CSV * /
var csv_data = array_data.map (function (l) {return l.join (',')}). join ('\ r \ n');
var blob = new Blob ([bom, csv_data], {type: 'text/csv'});
var url = (window.URL || window.webkitURL) .createObjectURL (blob);
var a = document.getElementById ('downloader');
a.download = 'data.csv';
a.href = url;
/ * Click to download * /
$('# downloader') [0] .click ();
});
});
</script>
</body>
</html>
-
Answer # 1
Related articles
- javascript - i want to download a file in sjis format using js (jquery)
- javascript - jquery doesn't work on heroku
- javascript - i want to implement scroll with slow and fast!
- javascript - i can't scroll the screen with jquery
- javascript - jquery fade-in/out animation implementation
- javascript - jquery doesn't work with loading scrit tag in bootstrap
- javascript - view limit with jquery cookie
- javascript - i want to output jquery html, but i can't use ruby (each statement)
- javascript - i want to like async with ajax
- javascript - dynamically change the value of transform scale by jquery pinch operation to scale elements
- javascript - jquery i want to select a specific element
- javascript - click an image to move to the next image jquery (mail order site product details screen)
- javascript - how to read a csv file like jsonstringify ()
- javascript - i want to download an audio file edited using the web auido api in the format of an audio file such as wav or mp3
- javascript - iterative processing does not work with jquery
- javascript - [jquery fullcalendar] dayclick doesn't work
- i want to write the jquery part in javascript
- javascript - jquery html creation
- javascript - i want to send a token with vue
- javascript - i get an error with split
Related questions
- javascript - a tag information does not appear in the link field
- javascript - i want to change the hamburger menu to a bento box
- javascript : Getting Input and Value
- javascript - i want to animate the characters that flow from left to right when reloaded
- javascript : Create picture from blocks
- javascript : Output to page from $ .getJson
- javascript : AOS Animate -disable on mobile
- javascript : Validation of one email or phone input at a time
- javascript : I want to add a multilingual switch button for asp.NET MVC localization
- javascript : How to hang a handler on a function using .each
The following parts
As you can see from
consle.log
here,text
isapple, 1,200, melon, 2,4000, banana, 4,500,
(String), array_data is["apple, 1,200, melon, 2,4000, banana, 4,500,"]
(naturally an array with only one string).You can see that the format is different from the successful array.
If you want to make use of the subsequent processing as it is, make sure that it is in the same format as a successful array.