代码之家  ›  专栏  ›  技术社区  ›  Pranav Mandlik

追加函数缺少参数列表时出现错误

  •  0
  • Pranav Mandlik  · 技术社区  · 8 年前

    我在尝试运行代码时遇到以下错误,我知道错误在append函数中,我不知道如何处理转义字符

    function check()
    {
        if(usertype==null)
        {
            alert('please select user type');
        }
        else
        {
            $('#registerusers').hide();
            if(usertype=='individual')
            {
                $('#agent').html();
                $('#vendor').html();
                $('#service').html();
                $('#'+usertype).append('<div class="tab">Name: <p><input placeholder="First name..." oninput="this.className = ''"></p> <p><input placeholder="Last name..." oninput="this.className = ''"></p> </div> <div class="tab">Contact Info: <p><input placeholder="E-mail..." oninput="this.className = ''"></p> <p><input placeholder="Phone..." oninput="this.className = ''"></p> </div> <div class="tab">Birthday: <p><input placeholder="dd" oninput="this.className = ''"></p> <p><input placeholder="mm" oninput="this.className = ''"></p> <p><input placeholder="yyyy" oninput="this.className = ''"></p> </div> <div class="tab">Login Info: <p><input placeholder="Username..." oninput="this.className = ''"></p> <p><input placeholder="Password..." oninput="this.className = ''"></p> </div> <div style="overflow:auto;"> <div style="float:right;"> <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button> </div> </div> <!-- Circles which indicates the steps of the form: --> <div style="text-align:center;margin-top:40px;"> <span class="step"></span> <span class="step"></span> <span class="step"></span> <span class="step"></span> </div> ');
                $('#'+usertype).removeClass('hid');
            }
            else if(usertype=='agent')
            {
                $('#individual').html();
                $('#vendor').html();
                $('#service').html();
    
                $('#'+usertype).html('<h2> as</h2>');
                $('#'+usertype).removeClass('hid');
            }
    
    
    
            alert(usertype);
        }
    
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Carsten Løvbo Andersen Dan Nemtanu    8 年前

    你必须逃离 '' 在代码中:

    像这样: oninput="this.className=\'\'"

    请看下面的解决方案:

    var usertype = "individual"
    function check() {
      if (usertype == null) {
        alert('please select user type');
      } else {
        $('#registerusers').hide();
        if (usertype == 'individual') {
          $('#agent').html();
          $('#vendor').html();
          $('#service').html();
          $('#' + usertype).append('<div class="tab">Name: <p><input placeholder="First name..." oninput="this.className=\'\'"></p><p><input placeholder="Last name..." oninput="this.className=\'\'"></p> </div> <div class="tab">Contact Info: <p><input placeholder="E-mail..." oninput="this.className=\'\'"></p> <p><input placeholder="Phone..." oninput="this.className=\'\'"></p> </div> <div class="tab">Birthday: <p><input placeholder="dd" oninput="this.className=\'\'"></p> <p><input placeholder="mm" oninput="this.className="></p> <p><input placeholder="yyyy" oninput="this.className=\'\'"></p> </div> <div class="tab">Login Info: <p><input placeholder="Username..." oninput="this.className=\'\'"></p> <p><input placeholder="Password..." oninput="this.className=\'\'"></p> </div> <div style="overflow:auto;"> <div style="float:right;"> <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button> </div> </div> <!-- Circles which indicates the steps of the form: --> <div style="text-align:center;margin-top:40px;"> <span class="step"></span> <span class="step"></span> <span class="step"></span> <span class="step"></span> </div> ');
          $('#' + usertype).removeClass('hid');
        } else if (usertype == 'agent') {
          $('#individual').html();
          $('#vendor').html();
          $('#service').html();
    
          $('#' + usertype).html('<h2> as</h2>');
          $('#' + usertype).removeClass('hid');
        }
        alert(usertype);
      }
    
    }
    
    check()
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="individual"></div>
        2
  •  1
  •   vikalp    8 年前

    尝试以下操作:

    function check()
    {
        if(usertype==null)
        {
            alert('please select user type');
        }
        else
        {
            $('#registerusers').hide();
            if(usertype=='individual')
            {   
                $('#agent').html();
                $('#vendor').html();
                $('#service').html();
                $('#'+usertype).append('\
                    <div class="tab">\
                        Name: <p><input placeholder="First name..." oninput="this.className = '+""+'"></p>\
                        <p><input placeholder="Last name..." oninput="this.className = '+""+'"></p> \
                    </div> \
                    <div class="tab">\
                        Contact Info: <p><input placeholder="E-mail..." oninput="this.className = '+""+'"></p> \
                        <p><input placeholder="Phone..." oninput="this.className = '+""+'"></p> \
                    </div> \
                    <div class="tab">\
                        Birthday: <p><input placeholder="dd" oninput="this.className = '+""+'"></p> \
                        <p><input placeholder="mm" oninput="this.className = '+""+'"></p> \
                        <p><input placeholder="yyyy" oninput="this.className = '+""+'"></p>\ 
                    </div> \
                    <div class="tab">\
                        Login Info: <p><input placeholder="Username..." oninput="this.className = '+""+'"></p> \
                        <p><input placeholder="Password..." oninput="this.className = '+""+'"></p> \
                    </div> \
                    <div style="overflow:auto;"> \
                        <div style="float:right;"> \
                            <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> \
                            <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button> \
                        </div> \
                    </div> <!-- Circles which indicates the steps of the form: --> \
                    <div style="text-align:center;margin-top:40px;"> \
                        <span class="step"></span> \
                        <span class="step"></span> \
                        <span class="step"></span> \
                        <span class="step"></span> \
                    </div> \
                ');
                $('#'+usertype).removeClass('hid');
            }
            else if(usertype=='agent')
            {
                $('#individual').html();
                $('#vendor').html();
                $('#service').html();
    
                $('#'+usertype).html('<h2> as</h2>');
                $('#'+usertype).removeClass('hid');
            }
    
    
    
            alert(usertype);
        }
    
    }