在检查单选按钮的评论中发表回答

时间:2015-04-24 19:22:36

标签: javascript jquery html

我希望以某种方式制作我的代码,以便一旦您选中正确的单选按钮(是/否),该问题就会显示在$("p:not(:has(b)):contains(" + val + ")").show().prev().show();中。如果选中了textarea单选按钮,我希望yes显示在yes中,并显示正确的问题textarea,如果Question 1单选按钮是选中,我希望no显示在no中。这就是我现在所拥有的:

HTML:

textarea

JQuery的:

Question 1 Yes<input id='chk1' value="yes" type='radio' />No<input name='chk3' value="no" type='checkbox' /><br />
Question 2 Yes<input id='chk2' value="yes" type='radio' />No<input name='chk3' value="no" type='checkbox' /><br />
Question 3 Yes<input id='chk3' value="yes" type='radio' />No<input name='chk3' value="no" type='checkbox' /><br />
Other comments<textarea id='comment'></textarea><br /><input type=submit name=submit value='Post' />

的jsfiddle:

http://jsfiddle.net/gk05pkkh/3/

3 个答案:

答案 0 :(得分:1)

问题在于,当您将代码设置为放在#comment中时,您不会提取复选框的值。改变这个:

$('#chk1').change(function(){
    $('#comment').text("Question 1:" + "");
});

到此:

$('#chk1').change(function(){
    var response = $(this).val();
    $('#comment').text("Question 1:" + response);
});

并使用单选按钮,如此处其他评论中所述。

答案 1 :(得分:1)

您应该在输入中使用“name”而非“id”,以便共享它们。

HTML:

Dim degrees() As String = IO.File.ReadAllLines("Degrees.txt")

DataGridView1.Rows.Clear() 'Remove any data the was previously populated.
DataGridView1.Columns.Clear() 'Remove any data the was previously populated.
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

For i = 0 To degrees.Count - 1
    If i = 0 Then 'if it's the first line in the file, create columns/headers.
        For Each s as String In degrees(i).Split(",")
            DataGridView1.Columns.Add(s, s)
        Next
        DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    Else 'otherwise just add a new row.
        DataGridView1.Rows.Add(degrees(i).Split(","))
    End If
Next

JQuery的:

Question 1 Yes<input name='chk1' value="yes" type='radio' />No<input name='chk1' value="no" type='radio' /><br />
Question 2 Yes<input name='chk2' value="yes" type='radio' />No<input name='chk2' value="no" type='radio' /><br />
Question 3 Yes<input name='chk3' value="yes" type='radio' />No<input name='chk3' value="no" type='radio' /><br />
Other comments<textarea id='comment'></textarea><br /><input type=submit name=submit value='Post' />

http://jsfiddle.net/gk05pkkh/24/

答案 2 :(得分:1)

我对自己想要做的事情采取了一些自由。对于这个HTML:

<div class="questionWrap">
    <span class="question">Question 1:</span>
    <label>Yes
        <input type="radio" name="q1" value="Yes"/>
    </label>
    <label>No
        <input type="radio" name="q1" value="No"/>
    </label>
</div>
<div class="questionWrap">
    <span class="question">Question 2:</span>
    <label>Yes
        <input type="radio" name="q2" value="Yes"/>
    </label>
    <label>No
        <input type="radio" name="q2" value="No"/>
    </label>
</div>

这个JS:

$('input[type="radio"]').change(function(){
   var parent = $(this).closest('.questionWrap'),
       text = parent.find('.question').text(),
       answer = parent.find('input[type="radio"]:checked').val(),
       answerStr = '';

    parent.attr('data-question', text  + answer);

    $('[data-question]').each(function(ind, ele){
        answerStr += ( $(ele).attr('data-question') +'\n');
    });

    $('textarea').text(answerStr);


});

See this functioning fiddle