0

我正在学习 jquery,所以我在这里创建了这个小提琴http://jsfiddle.net/8FXFE/17/

这是我的 html 代码

<div class="textForm">
<input type="radio" name="txtNumber" value="100" checked="checked" />100
<input type="radio" name="txtNumber" value="200" />200
<input type="radio" name="txtNumber" value="500" />500
<input type="radio" name="txtNumber" value="1000" />1000
<input type="radio" name="txtNumber" value="10000" />10000
<input type="radio" name="txtNumber" value="other" />other
<input type="text" name="other_field" id="other_field" onblur="checktext(this);"
/>
</div>
<div class="formText">
    <input type="radio" name="txtSpace" value="RJ" checked="checked"
    />Space 1.
    <br />
    <input type="radio" name="txtSpace" value="SM" />Space 2.
    <br />
</div>

<h3>Output:</h3>

这是CSS

#other_field {
 display: none;
}

这是jQuery

$(document).ready(function () {
console.log("parsed");
$("input[name='txtNumber'],input[name='txtSpace']").change(function () {
    $("#output").text("Changed to "+$("input[name='txtNumber']:checked").val() + " "                                    +$("input[name='txtSpace']:checked").val() + " +++++SOME FIXED VALUE OF TXTSPACE (i.e. SAY if RJ = 100 or if SM = 50) x VALUE OF TXTNUMBER++++++"
                    ); 
});
});

$(':radio').on('change', function () {
$('#other_field')[$(this).val() === 'other' ? 'show' : 'hide']();
});

$('#other_field').on('blur', function () {
var val = $(this).val();

if(isNaN(val)) {
alert('only numbers are allowed..');
}
else if(parseInt(val, 10) % 10 > 0) {
alert('only multiples of 10..');
}
});

我怎样才能实现那些在+++++++++++里面用大写字母显示的实际输出 TXTSPACE 的一些固定值(即如果 RJ = 100 或 SM = 50)x VALUE OF TXTNUMBER

另外我如何添加隐藏的 other_field 的动态值(如果选择)

4

1 回答 1

0
var value = $("input[name='txtNumber']:checked").val();
if(value == "other"){
    value = parseInt($("#other_field").val());
}
if(!value || isNaN(value)) value = 0;
var type = $("input[name='txtSpace']:checked").val();
var fixedMultiplier;
switch(type){
    case "RJ": fixedMultiplier = 100; break;
    case "SM": fixedMultiplier = 50; break;
    default: fixedMultiplier = 1; break;
}
var computedValue = value * fixedMultiplier;
$("#output").text("Changed to "+ value + " " + type + " (" + computedValue + ")");

http://jsfiddle.net/8FXFE/19/

Maybe you should also add a handler to the textfield to update output on keyup.

于 2013-02-10T13:44:24.397 回答