1

我有一个结果页面,向用户展示他们在网络表单上的得分情况。我正在尝试根据他们的回答显示不同的消息。

我已经设置了一个基本警报来显示用户选择的内容。

var q1 = $('#webform-component-fieldset-1--question-1').text();
alert(q1);

if ($('#webform-component-fieldset-1--question-1:contains("A. Yes")')) {
    $('.results-wrapper').hide();
    $('.results').append('<div class="popup">User Selected A. Yes, for the first answer.</div>');
}
else if ($('#webform-component-fieldset-1--question-1:contains("B. No")')) {
    $('.results-wrapper').hide();
    $('.results').append('<div class="popup">User Selected B. No, for the first answer.</div>');
}
else if ($('#webform-component-fieldset-1--question-1:contains("C. I do not know")')) {
   $('.results-wrapper').hide();
   $('.results').append('<div class="popup">User Selected C. I do not know, for the first answer.</div>');  
}
else {
   alert('something went wrong');
}

http://jsfiddle.net/Hr6Lj/10/

4

2 回答 2

1

我就是这样做的(使用 jQuery 的功能data())(演示

<div class="results">
    <div class="results-wrapper">
        <div class="form-item webform-component webform-component-display" id="webform-component-fieldset-1--question-1"
        data-answer="C">C. I do not know</div>
    </div>
</div>

<script>
  var updateAnswer = function (question) {
    var answer = question.data('answer');
    if (answer === 'A') {
      $('.results-wrapper').hide();
      $('.results').append('<div class="popup">User Selected A. Yes, for the first answer.</div>');
    } else if (answer === 'B') {
      $('.results-wrapper').hide();
      $('.results').append('<div class="popup">User Selected B. No, for the first answer.</div>');
    } else if (answer === 'C') {
      $('.results-wrapper').hide();
      $('.results').append('<div class="popup">User Selected C. I do not know, for the first answer.</div>');
    } else {
      alert('something went wrong');
    }
  }
  updateAnswer($('#webform-component-fieldset-1--question-1'));
</script>
于 2013-03-20T17:39:02.967 回答
0

以下将始终为真,因为对象始终为真。

if ($('#webform-component-fieldset-1--question-1:contains("A. Yes")'))

您想要知道是否选择了任何元素。

if ($('#webform-component-fieldset-1--question-1:contains("A. Yes")').length)
于 2013-03-20T17:32:02.457 回答