【问题标题】:Simplify multiple script简化多个脚本
【发布时间】:2017-08-04 09:25:01
【问题描述】:

请帮我简化这段代码我有一个测验项目,你选择一个答案,答案将附加到 .response 类


Appology 作为我之前的问题还不清楚。我是 jQuery 新手,因此非常感谢您的帮助。

这是场景,我在 div id="scenarioCont" 下有 28 组组项/字段集。 每个 div class="scenario"

有 7 个字段集
<div id="scenarioCont">
    <div class="scenario">
        <fieldset id="group1">
            <p>Question #1 </p> 
            <p><input type="radio" class="scenarioRadio" value="Strongly agree" name="group1" /><label>Strongly agree </label></p>
            <p><input type="radio" class="scenarioRadio" value="Somewhat agree" name="group1" /><label>Somewhat agree </label></p>
            <p><input type="radio" class="scenarioRadio" value="Neither agree or disagree" name="group1" /><label>Neither agree or disagree</label></p>
            <p><input type="radio" class="scenarioRadio" value="Somewhat disagree" name="group1" /><label>Somewhat disagree</label></p>
            <p><input type="radio" class="scenarioRadio" value="Strongly disagree" name="group1" /><label>Strongly disagree </label></p>
            <p><input type="radio" class="scenarioRadio" value="Don’t know / Can’t say" name="group1" /><label>Don’t know / Can’t say</label></p>
        </fieldset>

        <fieldset id="group2">
            <p>Question #1</p>
            <p><input type="radio" class="scenarioRadio" value="Yes, always" name="group2" /><label>Yes, always </label></p>
            <p><input type="radio" class="scenarioRadio" value="Yes, usually" name="group2" /><label>Yes, usually</label></p>
            <p><input type="radio" class="scenarioRadio" value="Yes, sometime" name="group2" /><label>Yes, sometimes </label></p>
            <p><input type="radio" class="scenarioRadio" value="No" name="group2" /><label>No</label></p>
            <p><input type="radio" class="scenarioRadio" value="Don’t know" name="group2" /><label>Don’t know</label></p>
        </fieldset>

        <fieldset id="group6">...</fieldset>
        <fieldset id="group7">...</fieldset>
    </div>

    <div class="scenario">
        <fieldset id="group8">...</fieldset>
         ...
        <fieldset id="group14">...</fieldset>
    </div>



    <button type="button" style="float:left;" class="buttonStyle" id="prevScenario" disabled="true">Back</button>
    <button type="button" style="float:right;" class="buttonStyle" id="nextScenario" disabled="true">Next</button>  **will change to submit after the last slide/panel item
</div>

选择下一步会显示下一组问题。

<button type="button" style="float:right;" class="buttonStyle" id="nextScenario" disabled="true">Submit</button> 

然后选择 提交 开始投票计算。所有选择的答案都应显示在 span class="response"

<table id="resultsTable">
    <tr  id="group1">
        <td class="responseCell"><span class="response">(the answer in group 1 should display here)</span></td>
    </tr>

    <tr id="group2">
        <td class="responseCell"><span class="response">(the answer in group 2 should display here)</span></td>
    </tr>

    .....
</table>

提前谢谢你们!

【问题讨论】:

  • HTML 中的标识符必须是唯一的,先更正一下
  • IDK 你想做什么。请正确描述。你想做的。并把你的代码像(HTML、CSS、JQuery 等)

标签: jquery arrays simplify


【解决方案1】:

首先,id 属性对于每个 HTML 元素必须是唯一的,因此您必须解决这个问题。

唯一的id 将有助于获得响应并将其附加到正确的位置。

您可以循环所有单选按钮,如果选中其中任何一个,您可以将其值附加到&lt;span&gt;,它的name 属性等于&lt;fieldset&gt; 的唯一id包含问题。试试这个:

$("input[type='radio']").change(function() {

    var $this = $(this);
    if ($this.prop('checked')) {
        value = $this.val();
        question = $this.parent("fieldset").attr('id');
        $('span[name='+question+']').text(value);
    }

});

我为你做了一个Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多