无法在 jQuery 中拆分字符串

unable to split string in jquery

本文关键字:拆分 字符串 jQuery      更新时间:2023-09-26
 <table border="0"  class="commentbox">
   <tr>
     <td>
          <div id="comment-f78d0b00-a008-473d-b647-a4a103ee3778"></div>
         <input type="button" class='btnReply' id="reply-f78d0b00-a008-473d-b647-a4a103ee3778" value="Reply"/>
     </td>
 </tr>
</table>
  $(".commentbox .btnReply").live("click", function () {
            //  $(this).hide();
            // i = 1;
            id = $(this).attr("id").split("-")[1]
            alert(id);
            var strDiv = 
             "<input type='text' class='txtCmnt' id='txtReply-" + id + "' /> 
        <input type='button' class='btnSave' value='Save' id='btnSave-" + id + "' /> ";
            $("#comment-" + id).append(strDiv);

        });

我希望 F78D0B00-A008-473D-B647-A4A103EE3778 在分裂后出现,而不是警报正在给出 F78D0B00

我尝试更改id = comment_ f78d0b00-a008-473d-b647-a4a103ee3778 并拆分

id = $(this).attr("id").split("_")[1],but it doesnt work.

编辑

输入 - 容器-f78d0b00-a008-473d-b647-a4a103ee3778

拆分后输出 f78d0b00-a008-473d-b647-a4a103ee3778

实现

最终结果的一种稍微复杂的方法:

// splits the supplied string by the hyphen '-' character
var string = 'comment-f78d0b00-a008-473d-b647-a4a103ee3778'.split(/-/);
    // removes the zeroeth/first element from the string array
    string.shift();
    // joins the remaining elements from the string back together
var newString = string.join('-');
console.log(newString);​

JS小提琴演示。

<小时 />

编辑以将上述内容转换为函数:

function splitString(haystack, needle){
    if (!needle || !haystack){
        return false;
    }
    var string = haystack.split(needle);
    string.shift();
    return string.join(needle);
}
// the first argument is the string you want to work on,
// the second is the character you want to split on
// f is the variable that will hold the new string
var f = splitString('comment-f78d0b00-a008-473d-b647-a4a103ee3778','-');
console.log(f);​

JS小提琴演示。

引用:

  • join() .
  • shift() .
  • split()

如果字符串的格式不会改变,那么使用子字符串函数如何呢?

var str = "comment-f78d0b00-a008-473d-b647-a4a103ee3778";
var subStr = str.substring(str.indexOf("-") + 1);
alert(subStr);

返回:f78d0b00-a008-473d-b647-a4a103ee3778这是相同的jsfiddlehttp://jsfiddle.net/M2Ywy/

如果它适合您,那么您的代码将如下所示

var id = $(this).attr("id");
var subStr = id.substring(str.indexOf("-") + 1);
alert(subStr);
var strDiv =
"<input type='text' class='txtCmnt' id='txtReply-" + id + "' /> <input type='button' class='btnSave' value='Save' id='btnSave-" + id + "' /> ";
$("#comment-" + subStr).append(strDiv);

演示 jsFiddle

这应该这样做:

 var id = '';
 var strDiv = '';
 $(".btnReply").live("click", function () {
            //  $(this).hide();
            // i = 1;
            id = $(this).prev('div').attr("id").split("comment-")[1];
            alert(id);
            strDiv = "<input type='text' class='txtCmnt' id='txtReply-"+ id +"' /><input type='button' class='btnSave' value='Save' id='btnSave-"+ id +"' />";
            $("div[id*="+id+"]").append(strDiv);
});

你可以这样做,可能是更长的版本,但只有 3 行强。只需通过连字符"-" split()字符串,然后拼接第一个索引,然后使用 join() 方法重新连接整个字符串

示例:http://jsfiddle.net/eE48z/

$(document).ready(function(){
$(".commentbox .btnReply").click(function(){
var jid = $(this).attr("id").split("-");
jid.splice(0,1);
var id=jid.join("-");
alert(id);
var strDiv = "<input type='text' class='txtCmnt' id='txtReply-" + id + "' /><input type='button' class='btnSave' value='Save' id='btnSave-" + id + "' /> ";
        $("#comment-" + id).append(strDiv);
    });
});