在WordPress网站上,我有一个短代码,可以通过链接来更新表单的字段(更新数据库中条目的值)。
我的第一个简码(将值更新为“是”):[entry-update-field id=[id] field_id=208 value="Yes" label="List your entry!" class="classmadevisible"]
-> WordPress页面中的结果是:List your entry!
我的第二个简码(将值更新为No):[entry-update-field id=[id] field_id=208 value="No" label="Stop list your entry!" class="classnotvisible"]
-> WordPress页面中的结果是:Stop list your entry!!
如果单击此链接,它将数据库中的字段(条目)的值更新为是或否。
我的问题是:是否可以通过“切换”按钮执行此简短代码(链接)?如果切换无效,则执行“否”(第二个简码);如果切换按钮有效,则执行“是”(第一个简码)。
.onoffswitch {
position: relative;
width: 45px;
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
height: 16px;
padding: 0;
line-height: 16px;
border: 0px solid #CCCCCC;
border-radius: 26px;
background-color: #E0E0E0;
}
.onoffswitch-label:before {
content: "";
display: block;
width: 26px;
margin: -5px;
background: #F70303;
position: absolute;
top: 0;
bottom: 0;
right: 25px;
border-radius: 26px;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
background-color: #E0E0E0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label, .onoffswitch-checkbox:checked + .onoffswitch-label:before {
border-color: #E0E0E0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
right: 0px;
background-color: #3DA10F;
}
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch"></label>
</div>
这是非常肮脏的方式,但是:
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch"></label>
<div class="state-shortcodes" style="display: none">
<div class="on-state-shortcode">
[entry-update-field id=[id] field_id=208 value="Yes" label="List your entry!" class="classmadevisible"]
</div>
<div class="off-state-shortcode">
[entry-update-field id=[id] field_id=208 value="No" label="Stop list your entry!" class="classnotvisible"]
</div>
</div>
</div>
<script type="text/javascript">
jQuery(function($){
$('.onoffswitch').on('input.onoffswitch-checkbox', 'change', function(){
let state = $(this).prop("checked") == true ? 'on' : 'off';
$(this).closest('.onoffswitch').find('.' + state + '-state-shortcode a')..trigger('click');
})
});
</script>