【问题标题】:Script tags are removed by jquery脚本标签被 jquery 删除
【发布时间】:2020-12-18 16:07:07
【问题描述】:

当外部文件包含脚本标签时,如何将外部文件加载到 div 中。似乎有两个问题;从我读过的 jquery 中剥离脚本标签,第二个无法加载到 innerhtml。我已经切换到 jquery .load 但脚本标签的剥离仍然是一个问题。我该如何解决这个问题?谢谢

<form id = "login_user" action="login/login_process.php" method="post">

    <label for="login_username">Username:</label>
    <br>
    <input type="text" id="login_username" name="login_username" value="" placeholder="Required" maxlength="20" minlength="2" size="20" pattern="^[A-Za-z0-9_]{2,20}$" onkeyup="validate_field('Username', 'login_username_pattern_message', document.getElementById('login_username'))" autofocus required>
    <div class="tooltip">?
      <span class="tooltiptext"><script>document.write(username_requirement)</script></span>
    </div>
    <br>
    <p id="login_username_pattern_message" ></p>

    <label for="login_password">Password:</label><br>
    <input type="password" id="login_password" name="login_password" value="" placeholder="Required" maxlength="40" minlength="8" size="40" pattern="(?=^.{8,}$)(?=.*\d)(?=.*\W+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" onkeyup="validate_field('Password', 'login_password_pattern_message', document.getElementById('login_password'))" required> 
    <div class="tooltip">?
      <span class="tooltiptext"><script>document.write(password_requirement)</script></span>
    </div>
    <br>
    <p id="login_password_pattern_message" ></p>
    <input type="submit" name="login_submit" id="login_submit" value="Submit" disabled>
</form>
<script>
$(document).ready(function(){
      $("#login_user").on("submit", function(event){
        event.preventDefault();
        var url = $(this).attr("action");
        var request_method = $(this).attr("method");
        var form_data = $(this).serialize();
        
        $.ajax({
          url: url;
          type: request_method,
          data: form_data
        }).done(function(response){
          /*$("#login_combination_message").html(response);*/
          var msg = "";
          if(response==1)
          {
            msg = "Please Enter A Valid Username/Password Combination";
          }
          else
          {
            msg = "Username Or Password Character Requirement Error";
          }
          $("#login_combination_message").html(msg);
        });
      });
   });
</script>
<?php
include_once '../../login_helper/config.php';
include_once 'regexp.php';
include_once 'login_create_functions.php';
// DEBUG *********************************************************************
include 'login_create_user_debug.php';
// DEBUG ********************************************************************* 

$validate_username = $_POST['login_username'];
$validate_password = $_POST['login_password'];
$num_of_rows = 0;
$error_message = 0;

// validate uername and password patterns
validate_patterns(); // PHP Validation

// Is the username & password combination correct
if ($valid_username_pattern && $valid_password_pattern)
{
    // Prepare stmt to prevent SQL Injection
    $sql =  "SELECT user_id, user_password
             FROM User_Login
             WHERE user_username = ?
             ";

    $stmt = $con->prepare($sql);
    $stmt->bind_param("s", $validate_username);
    $stmt->execute();
    $stmt->store_result();
    $num_of_rows = $stmt->num_rows;
    $stmt->bind_result($user_id, $user_password);
    $stmt->fetch();
    $stmt->free_result();
    $stmt->close();

    if ($num_of_rows == 1 && password_verify($validate_password, $user_password))
    {
        //echo 'Username/Password Verified!';

        // DEBUG *********************************************************************
          helper_path();
        // DEBUG *********************************************************************

        // NOTE

?>

        <script>
            var url = "product_splash.html?message=0";
            window.location.replace(url);
        </script>

<?php

    }

    $error_message = 1;
    

}
else
{
    $error_message = 2;

}

// DEBUG *********************************************************************
//login_process_debug();
// DEBUG *********************************************************************


// RESPONSE TO AJAX

/*$return_value = username_password_error_ajax($error_message);*/

//echo $return_value;

echo json_encode($error_message);
    
exit();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="preconnect" href="https://fonts.gstatic.com"> 
<link href="https://fonts.googleapis.com/css2?family=Inter&family=Noto+Sans&family=Oxygen:wght@300;400;700&display=swap" rel="stylesheet">
<script type="text/javascript" src="js/ohs.js"></script>
</head>
<body>
    <div class="page_container">

        <div id="nav_bar">
        </div>

        <div id="header">
        </div>

        <div id="main">
        </div>

        <div id="footer">
        </div>

    </div>
</body>
<script>loadLink('splash/home_nav_bar.html', 'nav_bar')</script>
<!--<script>loadLink('splash/home_header.html', 'header')</script>-->
<script>loadLink('splash/home_main.html', 'main')</script>
<script>loadLink('splash/home_footer.html', 'footer')</script>
</html>

【问题讨论】:

  • 我认为问题在于:表单和 位于加载到主页中的
    内。那么当提交表单时
  • 经过更多测试:脚本标签是问题所在。如何使用 jquery 从包含脚本标签的外部文件将元素加载到页面?

标签: jquery tags


【解决方案1】:

尝试在 $.ajax({}) 之后返回 false

$(document).ready(function(){
      $("#login_user").on("submit", function(event){
        event.preventDefault();
        var url = $(this).attr("action");
        var request_method = $(this).attr("method");
        var form_data = $(this).serialize();
        
        $.ajax({
          url: url;
          type: request_method,
          data: form_data
        }).done(function(response){
          /*$("#login_combination_message").html(response);*/
          var msg = "";
          if(response==1)
          {
            msg = "Please Enter A Valid Username/Password Combination";
          }
          else
          {
            msg = "Username Or Password Character Requirement Error";
          }
          $("#login_combination_message").html(msg);
        });
        return false;
      });
   });

【讨论】:

  • 刚刚尝试添加 return false;结果相同。但感谢您的意见。
【解决方案2】:

您在 ajax 中的 url 参数后放置了一个分号,您应该使用逗号代替:

来自: url : url;

到: url : url,

【讨论】:

  • 感谢您的收获。我改变了它,仍然是相同的反应。 url 加载而不是返回的响应。我已经删除了所有内容并重写了很多次。希望它是简单的,但由于我是 Ajax Jquery 的新手,我可能只是缺少一些基本的东西。
猜你喜欢
相关资源
最近更新 更多
热门标签