- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,如果我的标题难以理解。
我有多个名为 date1、date2 的字段...在更改这些字段时,我希望运行完整的脚本。今天,我只是简单地复制了每个 dateN 的代码。
我想要一个更简洁的代码并使用下一个 JQuery 选择器:
$("input[id^=date]")
我面临的问题是我不知道如何将选中的元素重用到后续代码中。我试过 $(this) 但它不起作用,因为它似乎未定义。
编辑:根据要求,我用 $(this) 展示了一个例子。变量 day 保持为空。如果我使用与月份和年份相同的选择器,它就会起作用。完整的示例还没有使用 $(this),但它是为了表明我的 $(this) 不起作用。
$("#date1").on("change", () => {
// Check validity of the date
const regexDate = /\d{2}\/\d{2}\/\d{4}/gm;
if (regexDate.test($("#date1").val()) == false) {
// Date format not valid, we quit the function
$("#info1").removeAttr("title");
$("#info1").hide();
return;
}
//Slice date
var day = $(this).val().substring(0, 2);
var month = $("#date1").val().substring(3, 5);
var year = $("#date1").val().substring(6, 10);
$.get("http://192.168.1.6:3000/getDateHistory/" + day + "/" + month + "/" + year, (data) => {
data = JSON.parse(data);
// Only display if not empty
if (data.length > 0) {
historyOfSameDate = "Other records exist at the same date: ";
for (var i = 0; i < data.length; i++) {
historyOfSameDate += data[i].desc + " - " + data[i].amount;
if (i < data.length - 1) {
historyOfSameDate += " | "
};
}
$("#info1").attr({
"title": historyOfSameDate
});
$("#info1").show();
} else {
$("#info1").removeAttr("title");
$("#info1").hide();
}
});
<tr>
<td><input class="form-control datepicker" id="date1" placeholder="Enter date"></td>
<td style="vertical-align:middle;"><i id="info1" class="bi bi-info-square-fill"></i></td>
<td>
<select class="form-control" id="type1" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<!-- Options dynamically created -->
<td><input class="form-control" id="desc1" placeholder="Enter description"></td>
<td><input class="form-control" id="amount1" placeholder="Enter amount"></td>
<td>
<select class="form-control" id="payer1" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<!-- Options dynamically created -->
<td>
<select class="form-control" id="paymentsource1" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<!-- Options dynamically created -->
<td style="vertical-align:middle; color:orange;"><i id="warning1" class="bi bi-exclamation-triangle-fill" title="This record will be ignored because it either doesn't contain enough information or contains error(s)."></i></td>
</tr>
<tr>
<td><input class="form-control datepicker" id="date2" placeholder="Enter date"></td>
<td style="vertical-align:middle;"><i id="info2" class="bi bi-info-square-fill"></i></td>
<td>
<select class="form-control" id="type2" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<!-- Options dynamically created -->
<td><input class="form-control" id="desc2" placeholder="Enter description"></td>
<td><input class="form-control" id="amount2" placeholder="Enter amount"></td>
<td>
<select class="form-control" id="payer2" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<!-- Options dynamically created -->
<td>
<select class="form-control" id="paymentsource2" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<!-- Options dynamically created -->
<td>
谢谢
最佳答案
差异
“我试过 $(this) 但它不起作用,因为它似乎未定义。”
箭头函数无法识别 this
您需要使用命名函数或匿名函数。此外,还有第二个参数接受指定 this
的选择器。 :
// Named Function
$(selector).on(event, ".thisClassWillBeTHIS", functionName);
function functionName(e) {...
// Anonoymous Function
$(selector).on(event, "input", function(e) {...
// "input" means any `<input>` in selector
在下面的例子中,next()
没有像您预期的那样使用,事实上,.next()
仅用于演示目的。 .next()
如果应用于 <input>
( this
) 不会跳转到下一个单元格 ( <td>
)——您需要遍历出 this
的单元格然后使用 .next()
然后使用 .find()
等等。
与其遍历一行的每个单元格,不如利用所有这些 #id
s(尽管我不鼓励在大多数情况下使用 #id
)。每个#id
除了数字之外,所有东西都是一样的,所以你可以得到 #id
的 this
并剥离数字并将其存储在变量中以连接到该行中的其他元素:
const set = this.id.match(/\d{1,2}/g).toString();
$('#info'+set).attr('title')
<table>
包裹在 <form>
中并且事件处理程序绑定(bind)到 <form>
.关于祖先<form>
元素并使用表单事件(“更改”)可以通过使用 event delegation 为您提供更多控制权和 HTMLFormElement interface 这是一个普通的 JavaScript 接口(interface),具有简洁的语法和特殊功能(可选,如果您需要它,它会在那里):
$('form').on('change', ...
<output>
放在每个 #info*
之后显示消息,因为我不知道为什么 "title"
正在使用属性(即使用 .next()
的位置)。
OP 代码所剩无几,因为大多数都有未包含的依赖项(也不需要)。他所做的大部分更改都是为了演示目的,您应该保留的唯一内容是:
$('form').on('change', '.datepicker', function(e) {
const set = this.id.match(/\d{1,2}/g).toString();
/* Replace `$('info1') with... */
$("#info" + set)
然后包装 <table>
在<form>
如果你还没有。顺便说一句,如果你添加 type="date"
至 .datepicker
它会神奇地变成一个真正的日期选择器,并且不需要日期检查。
例子
const display = D => {
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
return new Intl.DateTimeFormat('en-US', options).format(D);
};
$("form").on("change", '.datepicker', function(e) {
const set = this.id.match(/\d{1,2}/g).toString();
const regexDate = /\d{2}\/\d{2}\/\d{4}/g;
if ($(this).val() == '') {
$("#info" + set).next().html('');
return;
}
if (regexDate.test($(this).val()) == false) {
$("#info" + set).next().html(`<b>Invalid Date</b>`);
return;
}
const date = new Date($(this).val());
$("#info" + set).next().html(`<time>${display(date)}</time>`);
});
<form>
<table>
<tr>
<td><input class="form-control datepicker" id="date1" placeholder="MM/DD/YYYY"></td>
<td style="vertical-align:middle;"><i id="info1" class="bi bi-info-square-fill"></i>
<output></output>
</td>
<td>
<select class="form-control" id="type1" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<td><input class="form-control" id="desc1" placeholder="Enter description"></td>
<td><input class="form-control" id="amount1" placeholder="Enter amount"></td>
<td>
<select class="form-control" id="payer1" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<td>
<select class="form-control" id="paymentsource1" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<td style="vertical-align:middle; color:orange;"><i id="warning1"></i></td>
</tr>
<tr>
<td><input class="form-control datepicker" id="date2" placeholder="MM/DD/YYYY"></td>
<td style="vertical-align:middle;"><i id="info2" class="bi bi-info-square-fill"></i>
<output></output>
</td>
<td>
<select class="form-control" id="type2" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<td><input class="form-control" id="desc2" placeholder="Enter description"></td>
<td><input class="form-control" id="amount2" placeholder="Enter amount"></td>
<td>
<select class="form-control" id="payer2" placeholder="Enter type">
<option value=""></option>
</select>
</td>
<td>
<select class="form-control" id="paymentsource2" placeholder="Enter type">
<option value=""></option>
</select>
</td>
</tr>
</table>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
关于javascript - Use selected element when selector allow for multiple options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71272202/
我正在 Angular js和sails.js(node.js框架)之间的cors问题上挣扎 我尝试修复错误:XMLHttpRequest无法加载http://localhost:1337/en/au
我在我的应用程序中动态创建一个 iframe,结果如下所示: 这样的沙箱配置是否安全(特别是允许将 iframe 内容视为来自同一来源)? 最佳答案 正如 Namey 评论的那样,allow-sam
突然,似乎没有更改我的网络应用程序中的任何内容,我在 Chrome 中打开它时开始收到 CORS 错误。我尝试添加 Access-Control-Allow-Origin: *标题。然后我得到这个错误
我正在使用 Ionic Framework 开发应用程序。 在后端,我为 api 编写了一个 Flask 应用程序,如下所示: @API.route("/saverez",methods=["POST
我正在尝试从 onesignal api 发送 POST 请求 代码 axios({ method: 'post', url: 'https://onesignal.com/api/v1/no
我一直在寻找一些类似的问题来寻找答案,但我找不到。我有一个带有 express 的 node.js 服务器: app.use(function(req, res, next) { res.head
我使用 Google Cloud Functions 创建了一个 API 端点,并试图从 JS 获取函数中调用它。 我遇到了我很确定与 CORS 或输出格式有关的错误,但我不确定发生了什么。其他一些
当我尝试从我的 Angular 6 应用程序访问 Webhdfs 时,我收到如下所示的错误。在我看来,我几乎尝试了所有方法,包括更改 core-site.xml 和 hdfs-site.xml 中的设
我刚刚学习 ajax 和 cors 一些东西,现在我想知道 chrome 插件“allow-control-allow-origin”是如何工作的。 当我尝试执行正常的 ajax 请求时,控制台显示错
我正在努力理解如何允许用户在列表中创建新记录,但只允许创建者更新他们自己的帖子。 例如。以下结构: post { post1: { author: "user1"
我们的网络上有 2 个内部(内联网)Windows 服务器,仅适用于本地网络。在 server1 上安装了 Spark,我们可以在其中查询 Jabber 信息,如下所示: http://server1
所以在 routing.yml 中我定义了以下路由,以便编辑和删除特定设置: 路由.yml: settings.editDefaults: path: settings/{id}/d
我哪里出错了 title $.get("http://api.angel.co/1/tags/1654/startups?callback=aaa", function(data
首先,对您可能犯的语法错误表示歉意。我的英语不是很好。 我是 Spring 新手,我正在尝试创建基本身份验证安全性。 我正在尝试配置一个端点具有公共(public)访问权限,而其他端点则具有用户访问权
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: NULL in MySQL (Performance & Storage) 如果出现以下情况,您是否强烈建议取消选中
我正在尝试将我的一个网站中的内容加载到另一个网站中: $('#include-from-outside').load('http://lujanventas.com/plugins/banne
这里出了什么问题? OPTIONS https://twitter.com/oauth/request_token 401 (Unauthorized) jsOAuth-1.3.4.js:483 XM
allow-control-allow-origin : * header 存在于 API 响应中,但浏览器仍显示错误。网络 403 错误。 这是来自 API 的示例响应 header : Acces
在 Go 中制作 slice 时的 capacity 参数对我来说意义不大。例如, aSlice := make([]int, 2, 2) //a new slice with length and
Gitlab 保护分支中“允许推送”和“允许 merge ”是什么意思 最佳答案 引用 Gitlab Documentation here Using the "Allowed to push" an
我是一名优秀的程序员,十分优秀!