【问题标题】:HTML form actionsHTML 表单操作
【发布时间】:2011-06-16 22:59:10
【问题描述】:

对于输入样式“文本”,我有一个 javascript 函数,例如:

document.getElementById("dds1").onkeyup = function() {

    updateIt();
};

现在,如果我有一个复选框怎么办?代码会是什么样子?单选按钮呢?我将使用什么来获取值(对于复选框,选中或未选中。对于单选按钮,我想获取单击了哪个按钮)。

这是单选按钮的代码:

<li id="foli517"        class="     ">
<label class="desc" id="shippingChoice" for="Field517_0">
    Shipping Options
        </label>
<div>
<input id="shippingChoice" name="Field517" type="hidden" value="" />
    <span>
<input id="shipping1"       name="Field517"         type="radio"        class="field radio"         value="$2.00 Shipping Fee"      tabindex="13"                        checked="checked"                      />
<label class="choice" for="Field517_0"      >
    $2.00 Shipping Fee</label>
    </span>
    <span>
<input id="Field517_1"      name="Field517"         type="radio"        class="field radio"         value="I will pick up the items (free shipping)"        tabindex="14"                               />
<label class="choice" for="Field517_1"      >
    I will pick up the items (free shipping)</label>
    </span>
    </div>
</li>

【问题讨论】:

    标签: javascript html forms text input


    【解决方案1】:

    对于复选框:

    document.getElementById("checkBox").onclick = function() { 
        var isChecked = this.checked; 
        //whatever
    };
    

    对于单选按钮:

    document.getElementById("radioButton").onclick = function() { 
        var clickedId = this.id;
        //whatever 
    };
    

    【讨论】:

    • 我更新了代码。请看上面。如果用户选择了与“$2.00 shipping fee”相对应的单选按钮,您是否可以实现执行以下操作的功能: document.getElementById("shippingSpan").innerHTML = "$2.00";
    • @resing1 您可以通过多种方式进行操作 - 假设您在每个单选按钮上都有一个 id,最简单的方法是 document.getElementById("aSpecificRadioButton").onclick = function() { document.getElementById("shippingSpan").innerHTML = "$dollarAmountForThatRadioButton"; };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2019-09-11
    • 1970-01-01
    • 2013-04-22
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多