javascript - 选择器在 jquery 中不起作用

标签 javascript jquery

嗨,我有 HTML 代码

<div id="testing"></div>
<input type="text" name="amount[]" id="amount_1" value="800">
<input type="text" name="date[]" id="date_1" value="12/05/2015">
<input type="text" name="notes[]" id="notes_1" value="This is test notes">

<hr />
<hr />

<hr />
<hr />

<input type="text" name="amount[]" id="amount_2" value="1500">
<input type="text" name="date[]" id="date_2" value="12/10/2015">
<input type="text" name="notes[]" id="notes_2" value="Towing amount paid Order ID 000000001">

<hr />
<hr />

<hr />
<hr />

<input type="text" name="amount[]" id="amount_3" value="1600">
<input type="text" name="date[]" id="date_3" value="12/09/2015">
<input type="text" name="notes[]" id="notes_3" value="Towing amount paid Order ID 000000002">

现在我想在我的注释字段中搜索一个值,即拖车金额支付订单 ID 000000001,我想清空这些字段,我的 javascript/jquery 代码是

$(document).ready(function() {
     if($("input[name^=notes]").val().indexOf("Towing amount paid Order ID ") > -1) {
         $("#testing").text('found it');
         /*var current = $("input[name^=notes]");
         var onePrevious = $(current).prev();
         var twoPrevious = $(current).prev().prev();
         current.attr('value', '');
         onePrevious.attr('value', '');
         twoPrevious.attr('value', '');*/
     } else {
         $("#testing").text('not found');
     }
});

但是这段代码给了我未找到消息,我的代码出了什么问题,我尝试了不同的选择器,但对我不起作用。

最佳答案

您可以使用 jQuery :contains 伪函数,它将找到包含所需文本的第一个元素

引用号:https://api.jquery.com/contains-selector/

代码:

if($("input[name^='notes']:contains('Towing amount paid Order ID ')")) {
    $("#testing").text('found it');
} else {
    $("#testing").text('not found');
}

演示:http://jsfiddle.net/La1bq789/

关于javascript - 选择器在 jquery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32526907/

相关文章:

javascript - 关于如何在同一页面上多次调用 javascript 函数的建议

javascript - 声明 Node.js 项目 : Is there only one "server file" in node. js?

javascript - 如何在编写 OOP JS 的同时使用 jQuery

javascript - 使用 javascript 将图案应用到 3D 图像

javascript - Bootstrap collapse.in 需要点击两次才能隐藏侧边栏

javascript - 过渡效果——尝试打造最简单的滑出式导航

javascript - 在我的应用程序登录页面上显示 git 标签(版本)

javascript - 如何知道用户是否完成了所有任务?

javascript - 将 css 内容属性的字体大小缩放到父级

javascript - plupload json 响应