gpt4 book ai didi

javascript - 将数据值从 HTML 传递到 JS

转载 作者:行者123 更新时间:2023-11-28 14:45:46 25 4
gpt4 key购买 nike

我正在显示多个表单,需要传递一个值,以便我知道哪个上传的项目属于哪个表单。

我需要将数据值从 HTML 表单传递到 jquery 脚本。

我有以下代码 - 您可以看到我包含的数据值 data-uploadValue="some-value" 以及脚本中的 {{ data- uploadValue}}

function uploadFile() {
var file = _("file1").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "/upload/{{ data-uploadValue }}"); //
ajax.send(formdata);
}
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" value="test_value" data-uploadValue="some-value" onchange="uploadFile()"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>

我怎样才能实现这个目标?

最佳答案

您可以使用 $('.file-input').data('uploadValue');$('.file-input') 获取 html 元素的 data 属性').attr('data-uploadValue');

编辑

您可以尝试使用 this 将元素传递给函数,然后在函数中使用 .getAttribute("data-uploadValue")。这将为您提供调用该函数的元素的数据属性。

function uploadFile(element) {
var file = _("file1").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
var uploadValue = element.getAttribute("data-uploadValue");
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "/upload/" + uploadValue); //
ajax.send(formdata);
}
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" value="test_value" data-uploadValue="some-value" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>

关于javascript - 将数据值从 HTML 传递到 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46413115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com