单击单选按钮时更改变量的值

时间:2016-02-25 03:13:14

标签: javascript html if-statement radio-button

我知道这可能非常简单,但我无法弄明白。我需要的是变量的价值"价格"单击单选按钮并在页面上显示时更改。感谢您的帮助。

这是放在头部

::Rails::Engine

这是放在html页面的正文中

<script>
var price = 500;
if (document.getElementById('wifi').checked) {
price = price + 200;
}
</script>

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>
        <head>
                <title>Checkbox Example</title>
                <meta name="viewport" content="width=device-width, initial-scale=1">
        </head>
        <body>
                <input type="radio" name="wifi_price" id="wifi" value="200">Wifi Price</input>
                <p id="priceP"></p>
                <script>
                        var priceP = document.getElementById('priceP');
                        priceP.innerHTML = "$100";
                        var wifi = document.getElementById('wifi');
                        wifi.addEventListener('click', function() {
                                var price = priceP.innerHTML.slice(1, priceP.innerHTML.length);
                                price = (price * 1) + 100;
                                priceP.innerHTML = "$" + price;
                        });
                </script>
        </body>
</html>

答案 1 :(得分:0)

您应该将价格变量存储在对象中,以便可以轻松访问和更新。

public class OffsetDateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        // try Rfc3339Pattern format
        var parseResult = OffsetDateTimePattern.Rfc3339Pattern.Parse(valueResult.AttemptedValue);

        if (parseResult.Success)
        {
            return parseResult.Value;
        }

        // try dd/MM/yyyy format
        parseResult = OffsetDateTimePattern.Create("dd/MM/yyyy", CultureInfo.CurrentCulture,
            OffsetDateTime.FromDateTimeOffset(DateTimeOffset.MinValue)).Parse(valueResult.AttemptedValue);

        if (parseResult.Success)
        {
            return parseResult.Value;
        }

        return null;
    }
}