混合两个javascript代码

时间:2013-04-28 21:47:20

标签: javascript

我拼命地花了几个小时来让这个2小脚本工作没有成功。如果我像这样使用它们,没有人工作,但如果我只包含一个没有问题。所以问题不在于代码本身,而在代码一起使用时会产生冲突。我尝试了十几种组合,并添加了一些其他声明,但也没有运气。


代码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 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

你想要吗?

答案 2 :(得分: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,则第二个代码块将运行。

答案 3 :(得分: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;
    }
}