javascript 功能不适用于单选按钮 onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26253248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:43:27  来源:igfitidea点击:

Function is not working on radio button onclick

javascriptforms

提问by Suresh Kota

JavaScript function (here f1()) is not getting called on radio button (here id=iprange) onclickevent.

JavaScript 函数(此处f1())未在单选按钮(此处 id= iprangeonclick事件上调用。

HTML

HTML

function f1() {
    alert('yes');
}
<form role="form">
    <div class="box-body">
        <label>
            <input type="radio" name="optionsRadios" id="fullscan1" value="option1" /> Full Scan
        </label>

        &nbsp;&nbsp;&nbsp;&nbsp;


<!-- "IP Range" is the radio button that should show a "yes" alert. -->
        <label>
            <input type="radio" name="optionsRadios" id="iprange" value="option2" onclick="f1();" /> IP Range

        </label>
        From:
        <input type="text" id="text1"> To:
        <input type="text" id="text2"> &nbsp;&nbsp;&nbsp;&nbsp;


        <label>
            <input type="radio" name="optionsRadios" id="subnet" value="option3" /> Subnet

        </label>
        <input type="text" id="text3"> &nbsp;&nbsp;&nbsp;&nbsp;


        <button class="btn btn-danger">Scan</button>
    </div>

</form>

回答by Martin Ernst

@Suresh Kota . At this point its impossible to give an satisfying answer, you'll have to do a step-by-step-investigation. Start with following and post your result here.

@Suresh 科塔。在这一点上,不可能给出令人满意的答案,你必须一步一步地进行调查。从关注开始并在此处发布您的结果。

<head>
    ....
    <script> // your script tag, I assume it looks like this
        $(document).ready(function() {
            /* some code */
        });
        // now include your function as last line outside document ready
        function f1() {alert('yes');};
    </script>
    <!-- Make sure this is the only script tag in your html -->
    ....
</head>

If that doesn't help, rename your function, e.g. function xxx() {alert('yes');};

如果这没有帮助,请重命名您的函数,例如 function xxx() {alert('yes');};

and same with onclick-attribute in input-tag: onclick="xxx()".

与 input-tag: 中的 onclick-attribute 相同onclick="xxx()"

When having no succes, try directly: onclick="alert('yes')"

当没有成功时,直接尝试: onclick="alert('yes')"

If that all not work, there's something inside your document.ready that manipulates the button, and you should post that code.

如果这一切都不起作用,则 document.ready 中有操作按钮的东西,您应该发布该代码。

回答by Abid Ali

<label onclick="red()"><input type="radio" name="colors" id="red">Red</label>
or
<label onclick="red()"><input type="radio" name="colors" id="red"></label> <label for="red">Red</label>

<label onclick="red()"><input type="radio" name="colors" id="red">Red</label>
或者
<label onclick="red()"><input type="radio" name="colors" id="red"></label> <label for="red">Red</label>

<script>
function red(){
 alert('i am red');
}
</script>

if you want to call onclick function on radio button then you can use like this, it will work for you

如果你想在单选按钮上调用 onclick 功能,那么你可以这样使用,它会为你工作

Note: we have to call on function on label instead of radio button

注意:我们必须在标签上调用函数而不是单选按钮

回答by Beri

Try this one:

试试这个:

<input type="radio" name="optionsRadios" onclick="handleClick(this);" value="option1" />
<input type="radio" name="optionsRadios" onclick="handleClick(this);" value="option2" />
...

<script>
function handleClick(myRadio) {
    alert('New value: ' + myRadio.value);
}
</script>