-5

我还不是一个敏锐的 jquery / javascript 编码器 - 所以我希望那里有一个善良的灵魂,他可以帮助我将 javascript 重写为 jQuery 脚本。

代码如下所示:

<script type="text/javascript">

function update_delivery_address()
{
    elm_true = document.getElementById('delivery_same_as_invoice_true');
    elm_false = document.getElementById('delivery_same_as_invoice_false');
    if (!elm_true.checked && !elm_false.checked)
    {
        elm_true.checked = true;
    }
    if (elm_true.checked)
    {
        document.getElementById('delivery_name').value = '';
        document.getElementById('delivery_att').value = '';
        document.getElementById('delivery_address').value = '';
        document.getElementById('delivery_zipcode').value = '';
        document.getElementById('delivery_city').value = '';
        document.getElementById('delivery_email').value = '';
        document.getElementById('delivery_name').disabled = true;
        document.getElementById('delivery_att').disabled = true;
        document.getElementById('delivery_address').disabled = true;
        document.getElementById('delivery_zipcode').disabled = true;
        document.getElementById('delivery_city').disabled = true;
        document.getElementById('delivery_email').disabled = true;
    }
    else
    {
        document.getElementById('delivery_name').disabled = false;
        document.getElementById('delivery_att').disabled = false;
        document.getElementById('delivery_address').disabled = false;
        document.getElementById('delivery_zipcode').disabled = false;
        document.getElementById('delivery_city').disabled = false;
        document.getElementById('delivery_email').disabled = false;
    }
}

update_delivery_address();
</script>
4

3 回答 3

2

轻松搞定。全部替换

document.getElementById('yourid')

$('#yourid')

全部替换

.value = ''

.val('')

全部替换

.disabled = true (or false)

.prop('disabled', true) // or false

全部替换

.checked = true (or false)

.prop('checked', true) // or false

和所有

.checked

.prop('checked')
于 2012-04-21T16:18:20.940 回答
0

首先,我会为所有输入字段分配一个类(如 .delivery 或其他),以便选择更容易,您可以按类选择:

$('.delivery')

有关更多信息,请参阅http://api.jquery.com/category/selectors/

And then set all of your attributes accordingly

于 2012-04-21T16:18:39.673 回答
-1

jQuery 用于在更短的时间内完成工作。例如你可以写

elm_true = $('#delivery_same_as_invoice_true').get(0);

代替

elm_true = document.getElementById('delivery_same_as_invoice_true');

尽管如此,代码仍然是(有效的)JavaScript——没有必要重写你的代码(除了可读性)。

如果你想重写你的代码,你可以替换

var_name = document.getElementById('someid');

var_name = $('#someid').get(0);

document.getElementById('someid').attribute = value

经过

$('#someid').attr("attribute",value)

例如

$('#delivery_name').attr('disabled',false);

于 2012-04-21T16:17:07.177 回答