2010-03-10 46 views
0

我只有一个函数添加另一行到表id =“tblOtherParties”,我工作的很好,但是当我添加第二个函数来做同样的事情时,表id =“ tblHospitalAction“我得到未终止字符串常量的错误,但我不明白为什么它与第一个函数相同。jquery两个函数创建行问题

任何帮助将非常感激。

我有以下功能

<script type="text/javascript"> 
var counter = 1; 
    function createNewRow (text1,text2){ 
    var first = counter++; 
    $('#tblOtherParties > tbody').append('<tr><td> 
    <input type="text" id="txtName_'+first+'" value="'+text1+'" /> 
    </td><td> 
    <input type="text" id="txtRole_'+first+'" value="'+text2+'" /> 
    </td><td> 
    <a class="deleteBtn"/> 
    </td></tr>'); 
    } 



var counterHA = 1; 
function createNewRowHA (text1,text2){ 
    var first = counterHA++; 
    $('#tblHospitalAction > tbody').append('<tr> 
    <td> 
    <input type="text" id="txtH_'+first+'" value="'+text1+'" /> 
    </td> 
    <td> 
    <input type="text" id="txtA_'+first+'" value="'+text2+'" /> 
    </td><td><a class="deleteBtn"/></td> 
    </tr>'); 
    } 

    $(document).ready(function(){ 
    $(".deleteBtn").live("click", function(){ 
    $(this).closest('tr').not(':only-child').remove(); 
    }); 

    $('#btnAddOther').click(function() { 
    createNewRow('',''); 
    }); 

    $('#btnAddHA').click(function() { 
    createNewRow('',''); 
    }); 

}); 
</script> 

而下面的表

<div > 
    <span style="float:left;padding-top:34px;"><b>Other involved parties</b></span> 
    <table style="float:left;" id="tblOtherParties"> 
    <thead> 
    <tr> 
    <td class="title">Name</td> 
    <td class="title">Role</td> 
    <td><input type="button" id="btnAddOther" value="Add"/></td> 
    </tr> 
</thead> 
    <tbody> 
    <tr> 
    <td><input type="text" id="txtName_0" size="35"/></td> 
    <td><input type="text" id="txtRole_0" size="35"/></td> 
    <td><a class="deleteBtn" /></td> 
    </tr> 
    </tbody> 
</table> 
</div> 

<div > 
    <span style="float:left;><b>Other involved parties</b></span> 
    <table style="float:left;" id="tblHospitalAction"> 
    <thead> 
    <tr> 
    <td class="title">Name</td> 
    <td class="title">Role</td> 
    <td><input type="button" id="btnAddHA" value="Add"/></td> 
    </tr> 
</thead> 
    <tbody> 
    <tr> 
    <td><input type="text" id="txtH_0" size="35"/></td> 
    <td><input type="text" id="txtA_0" size="35"/></td> 
    <td><a class="deleteBtn" /></td> 
    </tr> 
    </tbody> 
</table> 
</div> 

回答

2

是你的字符串中跨越的实际来源多行还是你只是美化他们在你的问题显示的目的?跨越多行的字符串需要可以在每个行结束,然后连接起来到下一个,或者你有逃跑的换行符:

// Unterminated string constant on the first line 
"this is a 
test string that spans multiple lines" 

// Escaping the newline character 
"this is a \ 
test string that spans multiple lines" 

// Concatenating each line: 
"this is a " 
+ "test string that spans multiple lines" 

第2和第3的最大区别在于,第二将保持字符串中的换行符,第三行不会除非您使用\n\r\n手动添加它们。第二个缺点是字符串分隔符之间的所有其他空白字符(空格,制表符等)都是字符串的一部分,这使得代码看起来很整齐。