jquery - 如何将此代码写入函数

时间:2012-06-05 16:05:52

标签: jquery function

我有三个模块/ sectinos基本上做同样的事情与一些细微的差异。我试图将主要部分写入jquery函数,所以我可以根据需要调用它,但我无法正确地构造它。有人可以告诉我如何构建这个吗?

这是我作为一个单独的功能需要的部分,然后被其他人调用

var mytextareaFld = $('#ta_holdAll');
        var fld_1 = $('#firstname').attr('name')+':  '+$('#firstname').val();
        var fld_2 = $('#lastname').attr('name')+':  '+$('#lastname').val();
        var fld_3 = $('#street_number').attr('name')+':  '+$('#street_number').val();
        mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);

jsfiddle: http://jsfiddle.net/justmelat/v4sZb/

下面的html / jquery代码:

<form>
    <p>First name: <input type="text" name="firstname" id="firstname" value="Paul"/></p>
    <p>Last name: <input type="text" name="lastname"  id="lastname" value="Ryan" /></p>
<p>Street #: <input type="text" name="street_number"  id="street_number" value="4605"/></p>
<p>Address: <input type="text" name="address" id="address"/></p>
<p>City: <input type="text" name="city" id="city" /></p>
<p>State: <input type="text" name="state" id="state"/>

<span id="myzip">Zip:</span> <input type="text" name="zip"  id="zip" />
    <br /><br />
<span id="allInfo">All Info:</span> <textarea id="ta_holdAll" rows="10" cols="30"></textarea><div id="charCnt"></div><br /><br />
    <input type="button" value="Add to Field" id="addField">
</form>

    $(document).ready(function(){
    $('#addField').on('click',function(){
        var mytextareaFld = $('#ta_holdAll');
        var fld_1 = $('#firstname').attr('name')+':  '+$('#firstname').val();
        var fld_2 = $('#lastname').attr('name')+':  '+$('#lastname').val();
        var fld_3 = $('#street_number').attr('name')+':  '+$('#street_number').val();
        mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);
    });

    $('#allInfo').on('click',function(){
        var mytextareaFld = $('#ta_holdAll');
        var fld_1 = $('#firstname').attr('name')+':  '+$('#firstname').val();
        var fld_2 = $('#lastname').attr('name')+':  '+$('#lastname').val();
        var fld_3 = $('#street_number').attr('name')+':  '+$('#street_number').val();
        mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);
        var $mytextareaFld = $('#ta_holdAll');
        var $outPutCount = $("#charCnt");
        var $ofText = " characters of 1000 remaining";
        var val = $mytextareaFld.val();
        var val2 = $outPutCount.text(val.length).append("<strong>"+$ofText+"</strong>");
    });

    $("#firstname,#lastname, #street_number").blur(function(){
        var mytextareaFld = $('#ta_holdAll');
        var fld_1 = $('#firstname').attr('name')+':  '+$('#firstname').val();
        var fld_2 = $('#lastname').attr('name')+':  '+$('#lastname').val();
        var fld_3 = $('#street_number').attr('name')+':  '+$('#street_number').val();
        mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);
    });
});

1 个答案:

答案 0 :(得分:0)

JS小提琴演示:http://jsfiddle.net/lucuma/jmhmE/1/

$(document).ready(function(){

    function AddStuff(event) {
          var mytextareaFld = $('#ta_holdAll');
        var fld_1 = $('#firstname').attr('name')+':  '+$('#firstname').val();
        var fld_2 = $('#lastname').attr('name')+':  '+$('#lastname').val();
        var fld_3 = $('#street_number').attr('name')+':  '+$('#street_number').val();
        mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);
    }

    $('#addField').on('click', AddStuff);

    $('#allInfo').on('click',function(event){
        AddStuff(event);
        var $mytextareaFld = $('#ta_holdAll');
        var $outPutCount = $("#charCnt");
        var $ofText = " characters of 1000 remaining";
        var val = $mytextareaFld.val();
        var val2 = $outPutCount.text(val.length).append("<strong>"+$ofText+"</strong>");
    });

    $("#firstname,#lastname, #street_number").blur(AddStuff);
});​