Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

javascript - HTML复选框onclick在Javascript中调用(HTML checkbox onclick called in Javascript)

I am having a bit of trouble trying to figure out how to get a certain part of my code to work.

(我在尝试弄清楚如何使我的代码的某些部分工作时遇到了一些麻烦。)

<input type="checkbox" id="check_all_1" name="check_all_1" title="Select All" onclick="selectAll(document.wizard_form, this);">
<label for="check_all_1" onclick="toggleCheckbox('check_all_1'); return false;">Select All</label>

This is my HTML which works as it should (clicking the text will click the box).

(这是我的HTML工作原理(单击文本将单击该框)。)

The javascript for it is pretty simple:

(它的javascript非常简单:)

function toggleCheckbox(id) {
    document.getElementById(id).checked = !document.getElementById(id).checked;
}

However I want the onclick to happen for the input when the label is what makes the checkbox to be clicked.

(但是,当标签是单击复选框的时候,我希望输入发生onclick。)

At this current time the onClick js does not go.

(在这个当前时间onClick js不会去。)

What is one suggestion on how to do this?

(关于如何做到这一点的一个建议是什么?)

I tried to add the onclick of the input to the onclick of the label but that doesn't work.

(我试图将输入的onclick添加到标签的onclick,但这不起作用。)

Any suggestions/solutions would be wonderful.

(任何建议/解决方案都会很精彩。)

  ask by Craig translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about putting the checkbox into the label , making the label automatically "click sensitive" for the check box, and giving the checkbox a onchange event?

(如何将checkbox 放入 label ,使标签自动“点击敏感”复选框,并给复选框一个onchange事件?)

<label ..... ><input type="checkbox" onchange="toggleCheckbox(this)" .....> 

function toggleCheckbox(element)
 {
   element.checked = !element.checked;
 }

This will additionally catch users using a keyboard to toggle the check box, something onclick would not.

(这将另外捕获用户使用键盘切换复选框,某些onclick不会。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...