我正在更改文本字段上的 keyup 事件以引用 Rails 中的下拉选择。这是函数调用:
$(document).ready(function(){
$('#survey_q1').keyup(calculate);
$('#survey_q2').keyup(calculate);
$('#survey_q3').keyup(calculate);
$('#survey_q4').keyup(calculate);
$('#survey_q5').keyup(calculate);
$('#survey_q6').keyup(calculate);
});
function calculate(e)
{
// Set each value as they come in
var q1 = parseInt($('#survey_q1').val());
var q2 = parseInt($('#survey_q2').val());
var q3 = parseInt($('#survey_q3').val());
var q4 = parseInt($('#survey_q4').val());
var q5 = parseInt($('#survey_q5').val());
var q6 = parseInt($('#survey_q6').val());
// Don't do the calculation until all values are present
if ( q1 > 0 && q2 > 0 && q3 > 0 && q4 > 0 && q5 > 0 && q6 > 0) {
$('#survey_agent_score').val(
parseFloat((((q2 + q3 + q4 + q5 + q6) + (1.5 * q1)) / 32.5) * 100).toFixed(2)
);
} else {
$('#survey_agent_score').val('');
}
}
这是更改之前的视图:
<table>
<h4>Survey</h4>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_q1">Question 1:</label></td>
<td><input id="survey_q1" name="survey[q1]" style="width:60px;text-align:left;" type="text" class="required" value="<%= survey['q1'] %>" /></td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_q2">Question 2:</label></td>
<td><input id="survey_q2" name="survey[q2]" style="width:60px;text-align:left;" type="text" class="required" value="<%= survey['q2'] %>" /></td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_q3">Question 3:</label></td>
<td><input id="survey_q3" name="survey[q3]" style="width:60px;text-align:left;" type="text" class="required" value="<%= survey['q3'] %>" /></td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_q4">Question 4:</label></td>
<td><input id="survey_q4" name="survey[q4]" style="width:60px;text-align:left;" type="text" class="required" value="<%= survey['q4'] %>" /></td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_q5">Question 5:</label></td>
<td><input id="survey_q5" name="survey[q5]" style="width:60px;text-align:left;" type="text" class="required" value="<%= survey['q5'] %>" /></td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_q6">Question 6:</label></td>
<td><input id="survey_q6" name="survey[q6]" style="width:60px;text-align:left;" type="text" class="required" value="<%= survey['q6'] %>" /></td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><label for="survey_agent_score">Agent Score:</label></td>
<td><input id="survey_agent_score" name="survey[agent_score]" style="width:90px;text-align:left;background-color:#D6D6D6;" type="text" class="required" value="<%= survey['agent_score'] %>" readonly /></td>
</tr>
</table>
这是我想在视图中将文本字段更改为的片段:
<td><%= select_tag "survey[q1]", options_for_select(Call::ANSWERS, @call.survey['q1']), promt: "Please select ..." %></td>
这是添加 Rails 片段时呈现的 HTML:
<td style="vertical-align: top; text-align: right;"><label for="survey_q1">Question 1:</label></td>
<td>
<select id="survey_q1" name="survey[q1]" promt="Please select ..."><option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
这样做是当我在文本字段中输入一个数值时,它会调用 keyup 事件来进行计算。在填写所有六 (6) 个字段之前,它将在计算文本字段中显示空白。我想将数值的文本输入字段更改为数字下拉列表。我想保留的所有其他功能。
就价值节省而言,那里没有问题。该事件确实使用键盘的向上/向下/输入触发。它只是不注册鼠标的选择。