jquery删除空间后的所有内容

时间:2015-05-25 15:06:16

标签: javascript jquery ajax

我试图在空格后删除我的字符串中的所有内容,如下所示:

value.baseOrSchedStartList[i].substring(0, value.baseOrSchedStartList[i].indexOf(' '))

但是我收到了这个错误:

  

未捕获的TypeError:无法读取未定义的属性“substring”

我做错了什么?这是一个ajax调用...这里是完整的ajax调用:

$.ajax({
    type: "GET",
    url: "/vendorProject/api/connection/getVendorItems?community=" + $("#communtiyDropdown").val(),
    dataType: 'json',
    cache: false,
    success: function (results) {
        var html;
        html = "<table border='1'>";
        html += "<tr>";
        html += "<th>Task</th>";

        $.each(results, function (key, value) {
            html += "<th>" + key + "</th>";
        });

        html += "<th></th>";
        html += "</tr>";

        for (i = 0; i < taskArray.length; i++) {
            html += "<tr>"
            html += "<td>" + taskArray[i] + "</td>";
            $.each(results, function (key, value) {
                html += "<td>" + value.baseOrSchedStartList[i].substring(0, value.baseOrSchedStartList[i].indexOf(' '))  + "</td>";
            });

            html += "<td><input type='checkbox' name='checkbox' id='checkbox' /></td>"
            html += "</tr>";
       }

       html += "</table>";

       $("#tableData").html(html);
    }
});

有问题的字符串是12-12-2015 08:00:00 AM,我正在寻找12-12-2015 ....没时间。

1 个答案:

答案 0 :(得分:1)

尝试substr:)

$("button").on("click",function(){
    
      var a = $("input").val();
      
      var cropped = a;
      
      if(a.indexOf(" ")>=0) cropped = a.substr(0,a.indexOf(" "));
    
      $("b").html(cropped);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type='text' placeholder='write something here and hit the button' style='width:60%;'>
<br>
<button>Ready!</button>
<br>
<div>
The result without space and after all : <b></b>
</div>