为什么" \ n"即使被告知不要创建新的一行?

时间:2016-02-07 01:18:30

标签: javascript newline

我遇到了" \ n"即使在被告知不要复制时也要创建一条线。修复可能是一些简单的事情,我只是因为某种原因没有看到。我很感激对此问题的任何意见或指导。

(请仅给我Javascript答案,因为我对jquery或其他方法不感兴趣)

       <script type="text/javascript">

        if (pullDownResponseE == "")
        {
        }
        else {
        var pullDownValuesE = document.getElementById("taskOne");
        var pullDownResponseE = pullDownValuesE.options[pullDownValuesE.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponseE;
        }


        if (pullDownResponseF == "")
        {
        }
        else{
        var pullDownValuesF = document.getElementById("taskTwo");
        var pullDownResponseF = pullDownValuesF.options[pullDownValuesF.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponseF;
        }

        </script>

正如您所看到的,如果我的下拉值等于&#34;&#34;那么pullDownResponseF和pullDownReponseE应该什么都不做。并且这部分大部分都有效,它不会执行任何其他代码,除了新行&#34; \ n&#34;一部分。

有谁能解释这里出了什么问题?

编辑:拥有更多代码可能会有所帮助。我只会包含必要部分,因为它太长了。

        <script type="text/javascript">

        function copyNotesTemplate()
        { 

        var stuffToCopy = document.getElementById('myForm').value;
        if(stuffToCopy.length > 1)
        {
        var stuffToCopy = "PT meets criteria" + "\n" + document.getElementById('myForm').value;
        }
        if(document.getElementById('noPtCriteria').checked)
        {
        var stuffToCopy = document.getElementById('noPtCriteria').value;
        }


        if (pullDownResponsee == "")
        {
        }
        else {
        var pullDownValuese = document.getElementById("taskOne");
        var pullDownResponsee = pullDownValuese.options[pullDownValuese.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsee;
        }


        if (pullDownResponsef == "")
        {
        }
        else{
        var pullDownValuesf = document.getElementById("taskTwo");
        var pullDownResponsef = pullDownValuesf.options[pullDownValuesf.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsef;
        }

        if (pullDownResponseg == "")
        {
        }
        else{
        var pullDownValuesg = document.getElementById("taskThree");
        var pullDownResponseg = pullDownValuesg.options[pullDownValuesg.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponseg;
        }

        var tempValues = document.getElementById('whatUpdate').value
        if(tempValues.length > 1) 
        {
        var stuffToCopy = stuffToCopy + "Updated" + " " + document.getElementById('whatUpdate').value + " ";
        }
        else{
        }

        var tempValuess = document.getElementById('whatInfo').value
        if(tempValuess.length > 1) 
        {
        var stuffToCopy = stuffToCopy + "per" + " " + document.getElementById('whatInfo').value + "\n";
        }
        else{
        }

        var tempValue = document.getElementById('whatDSCRP').value
        if(tempValue.length > 1) 
        {
        var stuffToCopy = stuffToCopy + document.getElementById('whatDSCRP').value + " " + "dscrp on Collection tube and trf was resolved using" + " ";
        }
        else{
        }


        var tempValue = document.getElementById('resolveIT').value
        if(tempValue.length > 1) 
        {
        var stuffToCopy = stuffToCopy + document.getElementById('resolveIT').value + " ";
        }
        else{
        }

        var tempValue = document.getElementById('tubeCorrect').value
        if(tempValue.length > 1) 
        {
        var stuffToCopy = stuffToCopy + "trf was" + " " + document.getElementById('tubeCorrect').value;
        }
        else{
        }


        if(stuffToCopy.length > 1)
        {
        var stuffToCopy = stuffToCopy + "\n" + document.getElementById('moreNotes').value;
        }
        else{
        }

        if (pullDownResponsesu == "")
        {
        }
        else{
        var pullDownValuesu = document.getElementById("mod33Apply");
        var pullDownResponsesu = pullDownValuesu.options[pullDownValuesu.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsesu;
        }

        if (pullDownResponsesb == "")
        {
        }
        else{
        var pullDownValuesb = document.getElementById("resultICR");
        var pullDownResponsesb = pullDownValuesb.options[pullDownValuesb.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsesb + "," + " ";
        }

        if (pullDownResponsesc == "")
        {
        }
        else{
        var pullDownValuesc = document.getElementById("moneyNCIS");
        var pullDownResponsesc = pullDownValuesc.options[pullDownValuesc.selectedIndex].value;
        stuffToCopy = stuffToCopy + pullDownResponsesc + " ";
        }

        if (pullDownResponsesd == "")
        {
        }
        else{
        var pullDownValuesd = document.getElementById("resultMMT");
        var pullDownResponsesd = pullDownValuesd.options[pullDownValuesd.selectedIndex].value;
        stuffToCopy = stuffToCopy + pullDownResponsesd;
        }


        if(stuffToCopy.length > 1)
        {
        var stuffToCopy = stuffToCopy + " " + "Reason:" + " " + document.getElementById('whyNotEligible').value;
        }
        else{
        }

        if (pullDownResponsesa == "")
        {
        }
        else{
        var pullDownValuesa = document.getElementById("testReleased");
        var pullDownResponsesa = pullDownValuesa.options[pullDownValuesa.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsesa;
        }
        window.clipboardData.setData('text', stuffToCopy);
        }
        </script>

如果有人在此示例中跳过填写注释字段或跳过下拉列表,那么它将不会像我预期的那样执行代码,但在复制时会创建一个新行:

taskOne已选中

(因为任务2未被选中,因此这里有额外的行)

任务三选择

如果跳过任务2,我希望在第一和第三任务之间没有额外的一行。像这样:

taskOne已选中

任务三选择

注意:我知道有其他{}是没有意义的,但它在视觉上有所帮助。

如果您愿意,我可以在我的工具中复制/粘贴时创建完全正确的剪辑:

示例1:http://imgur.com/wGO5vnT

示例2:http://imgur.com/UX1tG5S

以下是我的html示例:

<html lang="en">
  What tasks are needed for the case?
  <br />
  <select class="style3" id="taskOne">
  <option value=""></option>
  <option value="ABN needed">ABN needed</option>
  <option value="Auth needed">Auth needed</option>
  </select>
  </html>

2 个答案:

答案 0 :(得分:3)

不,它不会添加新行,请参阅:

stuffToCopy = "";
controlGroup = "a\nb";
pullDownResponseE = "";

if (pullDownResponseE == "")
{
}
else {
    var pullDownValuesE = "taskOne";
    var pullDownResponseE = "value";
    stuffToCopy = stuffToCopy + "\n" + pullDownResponseE;
}
alert("stuffToCopy:"+stuffToCopy+";(no new-line here)\ncontrolGroup:"+controlGroup);

我的猜测是你的html打印得很远,你从输入中获得的值最后会包含一个额外的换行符。尝试将html更改为1行,不使用换行符,然后再次测试。

而不是:

<option value="a
">b
</option>

尝试:

<option value="a">b</option>

答案 1 :(得分:1)

好吧所以我修复了它,应该使用全能的document.getElementById,而不是试图为我的if语句使用pullDownReponse ..

我只是改变了这样的if语句:

 if (pullDownResponseg == "")
 {
 }

对此:

if (document.getElementById("taskThree").value == "")
 {
 }

感谢真诚的帮助。 (和其他人的荒谬的非答案)