无法在 jQuery 中拆分字符串
unable to split string in jquery
<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);
});
});
相关文章:
- 通过跳过拆分字符在 javascript 中拆分字符串
- jQuery字符串使用split()方法在空格后拆分字符串
- Javascript-正则表达式,用于在未转义字符上拆分字符串,例如|,但忽略|
- 如何在JavaScript中按大小写拆分字符串
- 根据上次出现的、和_拆分字符串
- 拆分字符串数组(JavaScript)后未定义
- 在空行上拆分字符串
- 使用Regex拆分字符串,保留分隔符
- 使用jquery或regex,带点但不带点的拆分字符串位于单引号中
- Nashorn和Rhino:使用空白regexp拆分字符串
- Javascript:不使用.split()方法拆分字符串
- 如何使用正则表达式在javascript中拆分字符串
- Javascript在第一个空格上拆分字符串
- 首次出现时拆分字符串
- 如何使用正则表达式(.match())拆分字符串
- 使用正则表达式拆分字符串
- Javascript:拆分字符串,但仅使用 1 个结果
- Angularjs-拆分字符串问题
- angularjs在拆分字符串时解析语法错误
- 基于javascript中的AND OR逻辑拆分字符串