得到一个按钮的父分区 [英] getting the parent div of a button
问题描述
我试图让按钮1的父分区,并在该div使所有的投入(目前我有他们禁用
< DIV ID =包装>
< DIV ID =的GameInfo>
< ASP:文本框ID =gameTitle/>
< ASP:文本框ID =游戏类型/>
< ASP:文本框ID =gameprice/>
<输入类型=按钮ID =Button1的价值=使能的onClick =myFunc的()/>
< / DIV>
JavaScript的:
函数myFunc的(){
的setTimeout(函数(){
VAR输入=的document.getElementById(按钮1)parentNode.querySelectorAll(输入[类型=文本]')。
[] .forEach.call(输入功能(输入){
input.disabled = FALSE;
});
});
}
我试图让Button1的父节点,然后我真的不知道的做这件事的正确方法,但我希望所有的按钮是父DIV的输入,使但只有母公司股利按钮我点击。有什么建议?
我做了一点不同的(没有的setTimeout):
函数myFunc的(榆树){\r
对于(VAR输入= elm.parentNode.querySelectorAll('输入[类型=文本])\r
L = inputs.length\r
; L--\r
;输入[L] .disabled = FALSE //使能输入\r
); //结束循环\r
}
\r
< DIV ID =包装>\r
< DIV ID =的GameInfo>\r
<输入类型=文本ID =gameTitle禁用=禁用/>\r
<输入类型=文本ID =游戏类型禁用=禁用/>\r
<输入类型=文本ID =gameprice禁用=禁用/>\r
<输入类型=按钮ID =Button1的价值=使能的onclick =myFunc的(本)/>\r
< / DIV>\r
< / DIV>
\r
对于替代不支持的浏览器 querySelectorAll
(也可以支持禁用
)
函数myFunc的(榆树){\r
对于(VAR输入= elm.parentNode.getElementsByTagName('输入')\r
L = inputs.length\r
; L--\r
;输入[L] .type.toLowerCase()==='文本'和;&安培; (输入[L] .disabled = FALSE)\r
); //结束循环\r
}
\r
< DIV ID =包装>\r
< DIV ID =的GameInfo>\r
<输入类型=文本ID =gameTitle禁用=禁用/>\r
<输入类型=文本ID =游戏类型禁用=禁用/>\r
<输入类型=文本ID =gameprice禁用=禁用/>\r
<输入类型=按钮ID =Button1的价值=使能的onclick =myFunc的(本)/>\r
< / DIV>\r
< / DIV>
\r
的上述两种功能的想法是,你通过点击该元素(按钮)的功能:的onclick =myFunc的(本)
< BR>
这也将努力为多个div(行),而不需要硬code中的 ID
。
这应该让你开始!
I am trying to get the parent div of the 'button1' and enable all the inputs in that div (I currently have them disabled'
<div id="wrapper">
<div id="gameInfo">
<asp:TextBox ID="gameTitle"/>
<asp:TextBox ID="gameType" />
<asp:TextBox ID="gameprice"/>
<input type="button" id="button1" value="enable" onClick="myFunc()"/>
</div>
javascript:
function myFunc() {
setTimeout(function () {
var inputs = document.getElementById("button1").parentNode.querySelectorAll('input[type="text"]');
[].forEach.call(inputs, function (input) {
input.disabled = false;
});
});
}
I am trying to get the parent node of button1 and then I'm not really sure of the correct way of doing it but I want all the inputs in that parent div of the button to enable but only of the parent div of the button I clicked. Any suggestions?
I'd do it a little different (without the setTimeout):
function myFunc(elm){
for( var inputs = elm.parentNode.querySelectorAll('input[type="text"]')
, L = inputs.length
; L--
; inputs[L].disabled = false //enable inputs
); //end loop
}
<div id="wrapper">
<div id="gameInfo">
<input type="text" ID="gameTitle" disabled="disabled" />
<input type="text" ID="gameType" disabled="disabled" />
<input type="text" ID="gameprice" disabled="disabled" />
<input type="button" id="button1" value="enable" onclick="myFunc(this)" />
</div>
</div>
Alternative for browsers that don't support querySelectorAll
(and optionally support disabled
):
function myFunc(elm){
for( var inputs = elm.parentNode.getElementsByTagName('input')
, L = inputs.length
; L--
; inputs[L].type.toLowerCase()==='text' && (inputs[L].disabled = false)
); //end loop
}
<div id="wrapper">
<div id="gameInfo">
<input type="text" ID="gameTitle" disabled="disabled" />
<input type="text" ID="gameType" disabled="disabled" />
<input type="text" ID="gameprice" disabled="disabled" />
<input type="button" id="button1" value="enable" onclick="myFunc(this)" />
</div>
</div>
The idea of both the above functions is that you pass the element that is clicked (the button) to the function: onclick="myFunc(this)"
.
This will also work for multiple divs ('rows') without need to hardcode the id
.
This should get you started!
这篇关于得到一个按钮的父分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!