0

实际上,我的元框文本字段上有两个复选框,该文本字段上方还有两个元框文本字段。在我的主题选项中还有两个文本字段,我必须在其中放置一些 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="checkbox" id="b" />
<input type="textarea" id="e" />

主题选项文本字段:

<input type="textarea" id="f"  />
<input type="textarea" id="g"  />

我必须将 javascript 放在主题选项文本字段中,有点像这样:

<div>
<script type=text/javascript> name: 'My Name is/', name2: 'Your name is/', </script> 
</div>

现在真正的担心来了。我希望当我单击带有 id 'a' 的复选框时,带有 id 'f' 的主题选项文本字段中的代码将被放入带有 id 'e' 但几乎没有修改的元框文本字段中。我需要的修改是,来自具有 id 'c' 和 'd' 的元框文本字段的数据将首先添加到从具有 id 'f' 的主题选项文本字段中获取的代码中,这样的方式是id为'c'的元框文本字段的值被添加到“名称:我的名字是/(这里将是id为'c'的文本字段的值)”和id为'd的元框文本字段的值' 被添加到“name2:你的名字是/(这里将是带有 id 'd' 的文本字段的值)”。

我还在为这些复选框行为使用 jquery 代码。这是我的 jQuery 代码。

$(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 代码存在缺陷,因为我不希望我的元框文本字段具有 id 'c' 的 Hello world 或 Not Hello World 之类的值。如前所述,我想要该字段的值。请在这方面帮助我。我非常沮丧。

4

1 回答 1

1

首先,使用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('');
    }
});

这似乎可以处理您描述的场景。如果没有,请编辑您的问题以逐步解释每个复选框更改时应该发生的情况,以便我们可以相应地编写脚本。

于 2012-10-05T15:59:20.033 回答