显示'复选框仅在选中特定复选框时使用
Display a 'div' box only when a specific checkbox is selected
我有一个Wordpress插件的下一个表单,我想显示'div'框'价格'文本字段只有当复选框'不自由1'或'不自由2'被选中。我知道这可以用jQuery/javascript实现,但我不能自己做到这一点。有什么建议吗?
<form action="" method="post" class="adverts-form">
<fieldset>
<div class="adverts-field-select ">
<label for="advert_category">Categories</label>
<div class="adverts-multiselect-options">
<label class="adverts-option-depth-0" for="advert_category-0">
<input name="advert_category[]" value="1" id="advert_category-0" type="checkbox"> Free</label>
<br>
<label class="adverts-option-depth-0" for="advert_category-1">
<input name="advert_category[]" value="2" id="advert_category-1" type="checkbox"> Not free 1</label>
<br>
<label class="adverts-option-depth-0" for="advert_category-2">
<input name="advert_category[]" value="3" id="advert_category-2" type="checkbox"> Not free 2</label>
<br>
</div>
</div>
<div class="adverts-field-text" style="display: none;">
<label for="adverts_price">Price</label>
<input name="adverts_price" id="adverts_price" type="text">
</div>
</fieldset>
</form>
这就是我尝试过的,没有成功。
我添加了一个插件(display-price.php):add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_enqueue_script(
'display-price', // name your script so that you can attach other scripts and de-register, etc.
WP_PLUGIN_DIR . '/display-price.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
和display-price.js:
jQuery(document).ready(function($) {
var advertNodes = $('#advert_category-1, #advert_category-2 ');
var advertInput = $('.adverts-field-text');
advertNodes.click(function() {
if (!advertNodes.is(':checked')) {
advertInput.hide();
}
else {
advertInput.show();
}
});
})
文件display-price.php和display-price.js在同一个目录下(默认的Wordpress插件目录)
我认为选项应该是单选按钮而不是复选框。下面的代码将帮助您开始:
var advertNodes = $('#advert_category-1, #advert_category-2 ');
var advertInput = $('.adverts-field-text');
advertNodes.click(function() {
if (!advertNodes.is(':checked')) {
advertInput.hide();
}
else {
advertInput.show();
}
});
最后我得出了这个可行的解决方案:
// a form in a plugin (not mine):
<form action="" method="post">
<fieldset>
<div>
<label for="category">Categories</label>
<select id="category" name="category">
<option value="">Select Options ...</option>
<option value="2">Free</option>
<option value="4">Not free 1</option>
<option value="7">Not free 2</option>
</select>
</div>
<div>
<label for="price">Price</label>
<input name="price" id="price" type="text">
</div>
</fieldset>
</form>
// another plugin (*display-price.php*) (this is mine)
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_enqueue_script(
'display-price',
plugins_url( '/display-price.js' , __FILE__ ),
array('jquery')
);
}
// a jQuery (*display-price.js*) to display the 'div' box with
// the 'Price' text field only when the option 'Not free 1' or
// 'Not free 2' are selected
jQuery(document).ready(function($) {
var cat = ["4", "7"]; // option values for which the price field is displayed
$("#price").parent('div').hide();
$('#category').change(function(){
if($.inArray($('#category').val(), cat) > -1) {
$("#price").parent('div').show();
} else {
$("#price").parent('div').hide();
}
});
});
display-price.php和display-price.js文件位于同一个目录(默认的Wordpress插件目录)。
相关文章:
- Bootstrap使用复选框显示和隐藏id
- oncheck复选框显示主干中的数据
- AngularJS过滤器使用ng个重复复选框显示所有页面加载
- 在同一行中为每个复选框显示和隐藏文本字段
- 为每个选中的复选框显示相同的问题集.(PHP和JS/Jquery)
- 我需要有关根据复选框显示和隐藏数据的帮助
- 如何使用复选框显示表单的更多选项并更改表单的操作属性
- 将选中的复选框显示在一个部分中,未选中的复选框显示在另一个部分中
- jQuery / JavaScript 复选框显示多个和单个选择中选中的复选框的数量
- 如何根据选中的复选框显示表行
- 如何为不同的复选框显示不同的下拉列表,然后在选中多个复选框时显示不同的下拉列表
- 让复选框显示某些内容
- 根据复选框显示/隐藏表行
- 根据复选框显示文本框
- 如何使用javascript复选框显示警报消息
- JQuery过滤器复选框显示/隐藏元素
- 正在选中复选框显示名称
- 在Highcharts中使用按钮/复选框显示/隐藏数据的示例
- EXTJS复选框显示为[object object]
- 复选框显示在for循环中,如何在Javascript中获取getElementById