jQuery和主题选项
实际上,在我的元框文本字段上有两个复选框,在该文本字段上方还有两个元框文本字段。在我的主题选项中还有两个文本字段,我必须在其中放入一些html和javascript代码。这是我的代码: 元框文本字段和复选框:jQuery和主题选项,jquery,Jquery,实际上,在我的元框文本字段上有两个复选框,在该文本字段上方还有两个元框文本字段。在我的主题选项中还有两个文本字段,我必须在其中放入一些html和javascript代码。这是我的代码: 元框文本字段和复选框: <input type="textarea" id="c" value="Your Name" /> <input type="textarea" id="d" value="My Name" /> <input type="checkbox" id="a"
<input type="textarea" id="c" value="Your Name" />
<input type="textarea" id="d" value="My Name" />
<input type="checkbox" id="a" />
<input type="checkbox" id="b" />
<input type="textarea" id="e" />
显然,这个jQuery代码中存在缺陷,因为我不希望看到id为“c”的metabox文本字段的这些值,例如Hello world或Not Hello world。我想要这个字段的值,正如我前面解释的。请在这方面帮助我。我非常沮丧。首先,使用
jQuery
而不是$
。在WordPress环境中,jQuery以“noconflict”模式运行,因此$
变量不可用
其次,我将稍微重写您的事件处理程序:
jQuery('#a, #b').change(function () {
var $this = jQuery(this), // Get a handle on the checkbox we just clicked.
$c = jQuery('#c'), // Get a handle on the textbox.
$d = jQuery('#d'), // Get a handle on the textbox.
$e = jQuery('#e'), // Get a handle on the textbox.
$f = jQuery('#f'), // Get a handle on one of our default values.
$g = jQuery('#g'); // Get a handle on one of our default values.
if ($this.attr('id') == 'a' && $this.is(':checked')) {
// Clicking checkbox a will add the content of c and f and place it in e
// It will also uncheck checkbox b.
$e.val( $c.val() + ' ' + $f.val() );
$b.removeAttr('checked');
} else if ($this.attr('id') == 'b' && $this.is(':checked')) {
// Clicking checkbox b will add the content of d and g and place it in e
// It will also uncheck checkbox a.
$e.val( $d.val() + ' ' + $g.val() );
$a.removeAttr('checked');
} else {
$e.val('');
}
});
这似乎可以处理您描述的场景。如果没有,请编辑您的问题,一步一步地解释更改每个复选框时应该发生什么,以便我们可以相应地编写脚本。不幸的是,这不是一个真正的WordPress问题。是的,您的站点正在运行WordPress,您正在尝试编写WP屏幕脚本,但这完全是一个JavaScript/jQuery问题。所以我将关闭它,并将其迁移到堆栈溢出,它将在这里讨论主题。但是为了加快进度,我也留下了一个可能会帮助你的答案。非常感谢你的支持。好的,请检查这个澄清版本。这才是我真正需要的。
<div>
<script type=text/javascript> name: 'My Name is/', name2: 'Your name is/', </script>
</div>
$(function () {
$('#a, #b').change(function () {
var $a = $('#a'), $b = $('#b'), $c = $('#c');
if (this.id == 'a' && this.checked) {
$c.val('Hello World!');
$b.prop('checked', false);
} else if (this.id == 'b' && this.checked) {
$c.val('Not hello World!');
$a.prop('checked', false);
} else {
$c.val('');
}
});
});
jQuery('#a, #b').change(function () {
var $this = jQuery(this), // Get a handle on the checkbox we just clicked.
$c = jQuery('#c'), // Get a handle on the textbox.
$d = jQuery('#d'), // Get a handle on the textbox.
$e = jQuery('#e'), // Get a handle on the textbox.
$f = jQuery('#f'), // Get a handle on one of our default values.
$g = jQuery('#g'); // Get a handle on one of our default values.
if ($this.attr('id') == 'a' && $this.is(':checked')) {
// Clicking checkbox a will add the content of c and f and place it in e
// It will also uncheck checkbox b.
$e.val( $c.val() + ' ' + $f.val() );
$b.removeAttr('checked');
} else if ($this.attr('id') == 'b' && $this.is(':checked')) {
// Clicking checkbox b will add the content of d and g and place it in e
// It will also uncheck checkbox a.
$e.val( $d.val() + ' ' + $g.val() );
$a.removeAttr('checked');
} else {
$e.val('');
}
});