简体   繁体   English

单击单选按钮时如何更改背景颜色

[英]How can I change the background color when I click on a radio button

JavaScript JavaScript的

<script>
function changeColor() {
    var element = user.elements["group1"];

    for (var i = 0; i < element.length; i++) {
        if (element[i].checked == true) {

            var newColor = element[i].value;
            alert("hai");
            document.getElementById("changeColor").style.background = newColor;

        }
    }
}​
</script>

HTML HTML

<div id="color"> 
    <input type="radio" name="group1" id="color1" value="#990000" /><label     for="color1">Red</label>
    <input type="radio" name="group1" id="color2" value="#009900" /><label for="color2">Green</label>
    <input type="radio" name="group1" id="color3" value="#FFFF00" /><label for="color3">Yellow</label><br><br><br>
    <button onclick="changeColor()">Change</button>

The above HTML and JavaScript code is fine when I click on the radio button the background color is changed. 当我单击单选按钮更改背景颜色时,上面的HTML和JavaScript代码就可以了。 It is working properly. 它工作正常。 However, my problem is that after the color change the browser will automatically refresh, which I do not want. 但是,我的问题是,更改颜色后,浏览器将自动刷新,这是我不希望的。

After click on submit button the onclick event will call. 单击提交按钮后,将调用onclick事件。 So if you send boolean value return false then the page is not submit here is code i am using return false; 因此,如果您发送布尔值return false,则页面不在此处提交,这是我正在使用的代码return false;

<script>
function changeColor() {
    var element = user.elements["group1"];
    for (var i = 0; i < element.length; i++) {
        if (element[i].checked == true) {

            var newColor = element[i].value;
            document.getElementById("changeColor").style.backGround = newColor;
        return false;
        }
    }
}
</script>

Change the onlick event add return before your changeColor() function 在您的changeColor()函数之前更改onlick事件add return

<button onclick="return changeColor();">Change</button>

Its looks like your button is the only button in the form so the browser is thinking its the submit button for the form. 它看起来像您的按钮是表单中的唯一按钮,因此浏览器认为它是表单的提交按钮。

As pointy says you can add a type to the button, add you onclick function to a different element such as a link (with an anchor not an actual link of course otherwise you will get the same problem) or move the button outside of the form. 尖锐地说,您可以向按钮添加类型,将onclick函数添加至其他元素(例如链接(当然不是锚点,否则为实际链接,否则您将遇到相同的问题)),或者将按钮移出表单。

I highly suggest you use jQuery : 我强烈建议您使用jQuery

HTML HTML

<button id="btnColor">Change</button>

jQuery: jQuery的:

$('#btnColor').live('click', function(e) {
    e.preventDefault();
    $('#changeColor').css('background-color', $('input:radio[name=group1]:checked').val());
});

You could wrap the radio-buttons with a form, like this, leaving the button outside: 您可以将单选按钮包装成这样的形式,将按钮放在外面:

<form name="form_radiobuttons">
  <input type="radio" name="group1" id="color1" value="#990000" /><label     for="color1">Red</label>
  <input type="radio" name="group1" id="color2" value="#009900" /><label for="color2">Green</label>
  <input type="radio" name="group1" id="color3" value="#FFFF00" /><label for="color3">Yellow</label><br><br><br>
</form>
<input type="button" onclick="changeColor()">Change</button>

This way the form won't be submitted when you click the button. 这样,当您单击按钮时将不提交表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM