提问人:Alex 提问时间:10/15/2019 最后编辑:Alex 更新时间:10/15/2019 访问量:497
错误:语法错误,无法识别的表达式:#whitelist_end option[value*= + value + ]
Error: Syntax error, unrecognized expression: #whitelist_end option[value*= + value + ]
问:
我遇到了语法错误,但主要是因为我自己不知道如何编写它。
我试过了,但我真的被困住了。虽然我觉得我很接近。
我的代码如下:
function hourselect_link_field(source_select, target_select){
var source_value = $('#' + source_select + ' :selected').val();
var time = parseInt(source_value.replace(":", ""));
var time_values = $("#" + target_select + " option").map(function () {
return this.value;
}).get();
$.each(time_values, function (index, value) {
var set_time = parseInt(value.replace(":", ""));
if (set_time <= time) {
$("#" + target_select + " option[value*= + value + ]").prop('disabled', true);
$("#" + target_select + " option[value*= + value + ]").addClass('disabled');
}
});
// console.log('time values = ' + time_values);
//alert(time);
}
我的问题基本上出在
$("#" + target_select + " option[value*= + value + ]").prop('disabled', true);
$("#" + target_select + " option[value*= + value + ]").addClass('disabled');
更具体地说:
option[value*= + value + ]"
我似乎无法获得正确的语法。
如果值有:“00:00”,我希望将其设置为禁用。 但它不起作用。
我收到这个语法错误
尝试时:
$("#" + target_select + " option[value*= " + value + "]").prop('disabled', true);
它也不起作用。
那么我错过了什么?
编辑:回答
@Casper & @Bilal Siddiqui 提供的解决方案
$.each(time_values, function (index, value) { var set_time = parseInt(value.replace(":", "")); if (set_time <= time) { $(`#${target_select} option[value*='${value}']`).prop('disabled', true); $("#" + target_select + " option[value*='" + value + "']").prop('disabled', true); } });
答:
0赞
Bilal Siddiqui
10/15/2019
#1
为什么不试试字符串文字:
$(`#${target_select} option[value*='${value}']`).prop('disabled', true);
$(`#${target_select} option[value*='${value}']`).addClass('disabled');
另一种方法
$("#" + target_select + " option").each(function () {
var set_time = parseInt(this.value.replace(":", ""));
if (set_time <= time) {
$(this).prop('disabled', true);
$(this).addClass('disabled');
}
});
评论
0赞
Alex
10/15/2019
我猜你的意思是而不是 $( => ' (or) ”。但两者都给了我另一个语法错误。错误:语法错误,无法识别的表达式:#${target_select} option[value*=${value}] jquery-3.4.1.min.js:2:13061 现在感觉很愚蠢。不好意思。#${target_select}
0赞
Bilal Siddiqui
10/15/2019
此外,我还添加了另一种方法,以便您可以避免同时使用 map 和 loop。
0赞
Alex
10/15/2019
我做到了,但它确实是“那些”。但出于某种原因,这些人拒绝了这项工作。我仍然不知道为什么。$().prop('禁用', true);不过这有效#${target_select} option[value*='${value}']
0赞
Bilal Siddiqui
10/15/2019
ES6 支持反引号。
0赞
Casper
10/15/2019
#2
您可以使用旧方法或 ES6 模板文本。
let select1 = 'sel1';
let select2 = 'sel2';
let val = '00:00';
$(`#${select1} option[value*='${val}']`).prop('disabled', true).addClass('disabled');
$("#" + select2 + " option[value*='" + val + "']").prop('disabled', true).addClass('disabled');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="sel1">
<option value="10">00:10</option>
<option value="00:00">00:00</option>
</select>
<select id="sel2">
<option value="10">00:10</option>
<option value="00:00">00:00</option>
</select>
评论
00:00
$("#" + target_select + " option[value*=" + value + "]").prop('disabled', true);
=
#${target_select} option[value*='${value}']
=