如何在textarea中关闭脚本标记之前添加文本输入?

时间:2013-06-14 00:51:10

标签: javascript

我正在使用rsform pro,并希望我的客户能够轻松地将限制日期添加到javascript日历中。我创建了一个文本输入框,将用户输入传输到textarea。以下是我到目前为止的情况:

function transfer() {
    var x = document.getElementById("txtT").value;
    document.getElementById("JS").value += '\n' + 'if(param1.indexOf(\'' + x + '\') === 0) \n\{ \n alert("We are almost booked up for this day.  Please call xxx-xxx-xxxx to make reservations."); \n return false; \n\}\n'
};

这是textarea的内容

<script type="text/javascript">
function rsfp_onSelectDate(param1)
    {
if(param1.indexOf('Sunday') === 0)
{
    alert("Sorry.  We are closed on Sundays & Mondays.");
    return false;
}
    if(param1.indexOf('Monday') === 0)
{
    alert("Sorry.  We are closed on Sundays & Mondays.");
    return false;
}    
else return true;
}
</script>

我想要做的是onClick将此传输到该行,否则返回true。

这可能吗?

1 个答案:

答案 0 :(得分:0)

see fiddle

function transfer() {
    var x = document.getElementById("txtT").value;
    var textarea = document.getElementById("JS");
    var toappend = '\n' + 'if(param1.indexOf(\'' + x + '\') === 0) \n\{ \n alert("We are almost booked up for this day.  Please call xxx-xxx-xxxx to make reservations."); \n return false; \n\}\n';
    var reg = /(?=else return true)/;
    var oldtext = textarea.value;
    var arr = oldtext.split(reg);
    var newtext = arr[0] + toappend + arr[1];
    textarea.value = newtext
};