选择器在jquery中不起作用

时间:2015-09-11 15:22:03

标签: 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">

现在我想在我的注释字段Towing amount paid Order 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');
     }
});

但是这段代码给了我not found消息我的代码中出了什么问题我尝试了不同的选择器,但是对我没用。

5 个答案:

答案 0 :(得分:3)

您可以使用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/

答案 1 :(得分:2)

此代码仅搜索具有值的输入 - This is test notes

要查看所有字段,请使用$.each

$(document).ready(function () {
    $("input[name^='notes']").each(function () {
        if ($(this).val().indexOf("Towing amount paid Order ID ") > -1) {
            $("#testing").text('found it');
        } else {
            $("#testing").text('not found');
        }
    });
});

JSFiddle - http://jsfiddle.net/FakeHeal/362o548n/

编辑以清除字段:http://jsfiddle.net/FakeHeal/362o548n/1/

答案 2 :(得分:0)

主要问题是您只检查一个元素值,并且需要检查所有元素。 我确实对你的代码进行了一些更改,现在它正在运行:

$("input[name^=notes]").each(function(){
  
if($(this).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');
}
  });
                       
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<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">

答案 3 :(得分:0)

$(document).ready(function() {
     $("input").each(function() {
       if ($(this).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', '');*/
     }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<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">

答案 4 :(得分:0)

$("input[name^=notes]").val()只返回页面中第一个元素的值,而不是匹配选择器。

为了检查所有这些,您需要查看每个实例

我建议你通过将每个组包装在一个容器中来模块化重复组,以帮助找到每个组中的其他相关元素

<div class="input-group">
    <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">
</div>

然后遍历您想要关注的输入

var hasNotes = $("input[name^=notes]").filter(function(){
      return this.value.indexOf("Towing amount paid Order ID ") > -1
}).length;
var message = hasNotes ? 'found it' :'not found'
$('#testing.text(message);

如果您需要调整其他值,请使用each lopp并在容器内遍历