0

I'm trying to understand why when I use a .prop() on a radio button for an if statement it will always return false. My goal is to have JS run only if the "#manualOverrideNo"radio button is selected. If anyone could enlighten me I would be very grateful. Here is a fiddle of a snippet of code - http://jsfiddle.net/gv0029/xEfw4/1/ - and here as well:

HTML: Manual Overide:

<label>No<input type="radio" name="manualOverride" 
id="manualOverrideNo" value="no" checked /></label>

<label>Yes<input type="radio" name="manualOverride" 
id="manualOverrideYes" value="yes" /></label>

<label>Footage:<input type="number" id="footage" 
name="footage" value="" /></label>  

<label>Post Quantity:<input type="postQuantity" 

name="postQuantity" id="postQuantity" value="" /></label> 

JS:

if($('#manualOverrideNo').prop('checked')) {
    //Code For Auto-complete

            //Quantity for Posts
            $('#footage').bind('keypress keydown keyup change', function(){
                var footage = parseFloat($(':input[name="footage"]').val(),10);
                var total = '';
                if(!isNaN(footage)){
                total = Math.ceil(footage /7);
                }
        $(':input[name="postQuantity"]').val(total.toString());
            });
}

Thanks again! Y'all are my hero!

4

1 回答 1

1

我已经改变了小提琴,它现在应该可以工作了

http://jsfiddle.net/xEfw4/5/

您需要在小提琴中使用 .prop 而不是 .attr,此外,您需要在操作发生后检查值,而不是在页面加载时检查值。

//Code For Auto-complete
//Quantity for Posts
$('#footage').bind('keypress keydown keyup change', function()
{
    if($('#manualOverrideNo').prop('checked')) 
    {
        var footage = parseFloat($(':input[name="footage"]').val(),10);
        var total = '';
        if(!isNaN(footage))
        {
            total = Math.ceil(footage /7);
        }
        $(':input[name="postQuantity"]').val(total.toString());
    }
});

http://api.jquery.com/prop/有关于属性和属性之间差异的信息。

于 2014-01-06T13:50:20.770 回答