0

我拼命地尝试了几个小时才能让这个 2 个小脚本工作但没有成功。如果我像那样使用它们,没有人可以工作,但如果我只包含一个没有问题。所以问题不在于代码本身,而在于代码一起使用时的冲突。我尝试了几十种组合,还添加了一些 else 语句,但也没有运气。


代码 1

<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
if (event=='reserve')
var mobile_number_param = document.getElementById('mobile_number_param');
mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
mobile_number_param.readOnly = true;
var email = document.getElementById('email');
email.readOnly = true;
var first = document.getElementById('first');
first.readOnly = true;
var last = document.getElementById('last');
last.readOnly = true;
}
// ]]></script>

代码 2

<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
if (event=='show_cart')
document.getElementById('empty_cart_block').style.display='none';
}
// ]]></script>

不幸的是,我不是编码员...有人有建议吗?

4

4 回答 4

1

尝试这样的事情:

<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
    if (event=='reserve') {
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
    }
    else if (event=='show_cart') {
        document.getElementById('empty_cart_block').style.display='none';
    }
}
// ]]></script>
于 2013-04-28T21:53:04.503 回答
1
<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
    if (event=='reserve') {
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
    }

    else if (event=='show_cart') {
        document.getElementById('empty_cart_block').style.display='none';
    }
}
// ]]></script>

你想要吗?

于 2013-04-28T21:53:23.197 回答
1

您可以将它们都移动到同一个函数中。on_my_form_loaded然后使用 if else 来确定应该执行哪个代码块。喜欢:

    <script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
    if (event=='show_cart')
    {
        document.getElementById('empty_cart_block').style.display='none';
    }
    else if (event=='reserve')
    {
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
    }
}

// ]]></script>

如果事件是,show_cart那么第一段代码将运行。如果事件是,reverse那么第二块代码将运行。

于 2013-04-28T21:53:27.490 回答
0

有更好的方法来处理这个问题,但请尝试使用 switch 语句:

function on_my_form_loaded(event) {
    switch (event) {
    case "reserve":
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
        break;
    case "show_cart":
        document.getElementById('empty_cart_block').style.display = 'none';
        break;
    }
}
于 2013-04-28T21:53:42.370 回答