【问题标题】:Adding dynamic content to dynamic content. Is it possible?将动态内容添加到动态内容。可能吗?
【发布时间】:2012-08-05 01:08:21
【问题描述】:

我正在为课程创建一个测验网站,但在格式化用户创建问题的页面时遇到了问题。我希望在用户单击单选按钮时弹出与特定问题类型有关的其他信息。然后,如果用户单击在初始附加信息中创建的按钮,我希望弹出更多附加信息。

所以它开始看起来像这样

然后它看起来像这样

然后一旦用户点击添加选项按钮几次,它看起来像这样

为了实现这一点,我尝试使用 jquery 添加新内容。但是,我似乎无法显示该内容。这是当前代码

在jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript"
        src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="QuestionsCreate.js"></script>
    <meta charset="UTF-8">
    <title>Quiz Creation</title>
</head>
<body>
    <h1>Create Your Quiz</h1>

    <form action="QuestionsCreateServlet" method="post">
        <h2>Question Type</h2>

        <input type="radio" name="type" class="type" id="multipleChoice"
            value="multipleChoice" />
        <label for="multipleChoice">Multiple Choice</label><br/>

        <input type="radio" name="type" class="type" id="fillBlank"
            value="fillBlank" />
        <label for="fillBlank">Fill-in-the-Blank</label><br/>

        <input type="radio" name="type" class="type" id="pictureResponse"
            value="pictureResponse" />
        <label for="pictureRsponse">Picture Response</label><br/>

        <input type="radio" name="type" class="type" id="textResponse"
            value="textResponse" />
        <label for="textResponse">Text Response</label><hr/>

        <div class="jspf"></div>

        <input type="submit" name="button" id="button" value="Finish" />
    </form>
</body>
</html>

在 javascript 中

$(document).ready(function() {
    $(".type").click(function(){
    $(".jspf").html("<jsp:include page='WEB-INF/" +
            $(".type").attr("value") + ".jspf' />");
    $("#button").attr("value", "Add");
    });

    var nOptions = 1;
    $("#add-option").click(function(){
    ++nOptions;
    $(".options").append("<input type='checkbox' name='option" +
            nOptions + "' value='" + nOptions + "' /> " +
            "<input name='name" + nOptions + "' /><br />");
    });

    var nBlanks = 1;
    $("#add-answer").click(function() {
    ++nBlanks;
    $(".fill-blank-answer").append("<input name='answer" + nBlanks +
            "' /><br/>");
    });
});

示例jspf

<h3>Question</h3>
<input name="prompt" />
<h3>Options</h3>
Check the options that denote the correct answer<br/>
<div class="options">
<input type='checkbox' name='option1' value='1' />
<input name='name1' /><br />
</div>
<input type="button" value="Add Option" id="add-option" /><hr/>

我也尝试过将 jspf 代码移动到 javascript 中,但这也不起作用。

有没有一种方法可以根据动态添加的内容向我的网页动态添加内容?提前致谢!

【问题讨论】:

  • 当然不会,你会在时空连续体中撕开一个洞,我们所知道的生命将不复存在。诚然这是最坏的情况——但另一方面,它可能会奏效。

标签: javascript jquery jsp web-applications


【解决方案1】:

$.get("${ctx}/store/terminalApply/applyTemplate?terminalType=${terminalType}&index="+num, function(data){ $("#contentDiv").append(data); });

数据是页面内容

【讨论】:

  • ${terminalType} 应该在双引号内吗?
【解决方案2】:

您遇到的问题是您试图将服务器端 JSP 标记注入客户端的浏览器。以这一行为例:

$(".jspf").html("<jsp:include page='WEB-INF/" +
        $(".type").attr("value") + ".jspf' />");

一旦执行了那行 javascript,客户端的浏览器现在就有了标记:

<div class="jspf"><jsp:include page="wEB-INF/pictureResponse.jspf" /></div>

浏览器不知道如何处理 标签,所以他们只是默默地忽略它们。

您需要做的是将您尝试包含的 jspf 映射到一个 url 并使用类似的东西:

$(".jspf").load("/fragments/pictureResponse.jspf");

$.load 将 AJAX 请求从客户端浏览器发送回服务器,从服务器检索一些 HTML,然后将其插入到与 CSS 选择器“.jspf”匹配的元素中。

您的初始点击处理程序也存在问题。

$(".type").attr("value")

$.attr 总是返回第一个匹配元素的属性值,所以无论用户点击什么,该行都会评估为“multipleChoice”。您可能想要做的是:

$(this).attr("value")

在点击处理程序的上下文中,“this”指的是用户刚刚点击的内容。

更新

加载次要内容后,我将如何添加“添加选项”点击处理程序:

$('jspf').load('/fragments/pictureResponse.jspf', function() {
    $('#add-option').click(function() {
        nOptions++;
        $('.options').append('<input type="checkbox" name="option' + nOptions +
        '" value="' + nOptions + '" />  <input name="name' + nOptions + '" /><br />");
    });
});

【讨论】:

  • 这适用于初始动态内容。通过将所有内容放入 javascript 中,我能够产生相同的结果。如何加载辅助动态内容?那部分可能吗?
  • 次要内容可能遇到的问题是,您试图在元素实际存在之前将事件处理程序附加到 DOM 元素,因为您试图将处理程序附加到document.ready 事件。我建议使用 $.load() 的第二个参数,这是一个回调,将在 AJAX 请求完成后执行。执行回调后,新元素将位于文档 DOM 中,因此您可以在其中添加新的事件处理程序。
猜你喜欢
  • 1970-01-01
  • 2014-01-18
  • 2014-09-13
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多