[英]JS function .hide
I have created a objects in .less, and i have created a few buttons in a php file, when the button is clicked in the php file I want some of the objects to hide. 我在.less中创建了一个对象,并且在php文件中创建了一些按钮,当在php文件中单击按钮时,我希望某些对象隐藏。 Here is my php code
这是我的PHP代码
<label for="select-pattern">Select</label>
<select name="" id="select" data-native-menu="false">
<option data-placeholder="true">Select</option>
<option name="asdf" value="pat">Select this</option>
<option name="fdas" value="tap">select now</option>
<select>
here is my JS code, 这是我的JS代码,
$(document).ready(function(){
$("#asdf").click(function(event){
//Your actions here
$('#object1, #object2').hide();
});
});
the less file is where i created the objects, here is the code for that 较少的文件是我创建对象的位置,这是该代码
#object1, #object2 {
width: 46px;
height: 23px;
background-color: #fff;
position: absolute;
left: 50%;
margin-left: -10px;
z-index: 10;
}
Why isn't the hide function not working? 隐藏功能为什么不起作用? is their an error in the code?
他们的代码中有错误吗?
your are having HTML not PHP. 您的HTML不是PHP。 I don't believe the click event is valid on options.
我认为click事件在选项上无效。 You have to make a button,and set asfd as id not the name of the element.
您必须创建一个按钮,并将asfd设置为id而不是元素的名称。
<input type="button" id="asdf" value="Hide objects">
Try this. 尝试这个。 (Assuming the object1 and object2 refers to html elements in your DOM).
(假设object1和object2引用了DOM中的html元素)。
You need a change event on the select and check its value. 您需要对select进行更改事件并检查其值。 If match, hide the object1 , object2.
如果匹配,则隐藏object1和object2。
<label for="select-pattern">Select</label>
<select name="" id="select" data-native-menu="false">
<option data-placeholder="true">Select</option>
<option name="asdf" value="pat">Select this</option>
<option name="fdas" value="tap">select now</option>
<select>
<div id="object1">Object 1</div>
<br>
<br>
<div id="object2">Object 2</div>
$().ready(function()
{
$("select").change(function(){
if($(this).val() == "pat")
$('#object1, #object2').hide();
else
$('#object1, #object2').show();
});
});
http://jsfiddle.net/9tvmac08/1/ http://jsfiddle.net/9tvmac08/1/
You can use this: 您可以使用此:
$(document).ready(function(){
$("#test").on("change", function(){
var id = $(this).val();
$(".objects #"+id).toggle();
});
});
Or, according to your code: 或者,根据您的代码:
$("#object1, #object2").toggle();
https://jsfiddle.net/8mxqvLwb/ https://jsfiddle.net/8mxqvLwb/
If you want to bind some event to option element in select, you have to bind it to select element. 如果要将某些事件绑定到select中的option元素,则必须将其绑定到select元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.