jQuery多个表单元素,复选框id和进行计算

时间:2011-08-19 18:40:25

标签: jquery

不确定从哪开始,所以我正在寻找正确的方向。

我有一个包含多个唯一复选框和相关文本输入的表单。在选中复选框时,还会有一个数学值应用于其中的每一个。 在创建jQuery函数时,任何帮助都是值得赞赏的:

示例模型:

enter image description here

<input type="checkbox" id="cb109179cb" /> 109179 - $4,390.30 <input type="text" name="applyto109179applyto" id="applyto109179applyto" value="0" readonly="readonly" /> <em>($4,389.30)</em>
<input type="checkbox" id="cb109246cb" /> 109246 - $4,231.76 <input type="text" name="applyto109246applyto" id="applyto109246applyto" value="0" readonly="readonly" /> <em>($4,231.76)</em>
<input type="checkbox" id="cb109314cb" /> 109314 - $3,934.40 <input type="text" name="applyto109314applyto" id="applyto109314applyto" value="0" readonly="readonly" /> <em>($3,934.40)</em>
<input type="checkbox" id="cb109381cb" /> 109381 - $4,240.52 <input type="text" name="applyto109381applyto" id="applyto109381applyto" value="0" readonly="readonly" /> <em>($4,240.52)</em>
<input type="checkbox" id="cb109447cb" /> 109447 - $4,261.30 <input type="text" name="applyto109447applyto" id="applyto109447applyto" value="0" readonly="readonly" /> <em>($4,261.30)</em>
<input type="checkbox" id="cb109514cb" /> 109514 - $3,460.55 <input type="text" name="applyto109514applyto" id="applyto109514applyto" value="0" readonly="readonly" /> <em>($3,460.55)</em>

1 个答案:

答案 0 :(得分:2)

$('#my_form input:checkbox[id^="cb"]').change(function(){
    var $this = $(this),
        $input = $this.next('input:text[id^="applyto"]');
    if($this.is(':checked')){
        // checkbox checked, apply your logic to $input
    } else{
        // checkbox un-checked, apply your logic to $input   
    }
});

Here is a demo