0

好吧,我自己真的很困惑。

我们有这个应用程序可以连接值并将它们发送到 TA 框。我想先确定该字段是否有值,

  • 如果确实有值,请将其添加到 TA 字段
  • 如果它没有值/空,则不要将其添加到 TA 字段中,

我该怎么做呢?

我想在模糊事件中使用它[参见代码示例]。我知道我需要使用.each(),检查长度和/或查看每个字段,看看是否有值,如果有,放入 TA 字段

有人可以告诉我如何做到这一点吗?

jsfiddle >> http://jsfiddle.net/justmelat/6JHRT/

HTML

<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"/><br />

<span id="myzip">Zip:</span> <input type="text" name="zip"  id="zip" />
<br /><br />
<select name="hometype" id="hometype">
<option value="">Select</option>
<option value="SFH">Single Family Home</option>
<option value="Condo">Condo</option>
<option value="Trailer">Trailer</option><br />
</select><br />
<input type="radio" name="ownership" value="own" /> Own
<input type="radio" name="ownership" value="rent" /> Rent
    <hr />
<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(){

    function combineFields(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();
        var hold_all_fields = fld_1 +'\n' + fld_2 +'\n' + fld_3;
      //hold_all_fields.each();
      mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);
  }

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

    $('#allInfo').on('click',function(event){
        combineFields(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(combineFields);
});

​</p>

4

2 回答 2

1

试试这个——只要检查字段是否存在(有长度),如果不存在,使用一个空字符串:

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

http://jsfiddle.net/6JHRT/3/

为了增加实用性,请用于jQuery.trim()消除空格:

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

http://jsfiddle.net/6JHRT/4/

于 2012-06-07T16:07:57.810 回答
0

试试这个:

    function combineFields(event) {
        $('#ta_holdAll').val('');
        $("#firstname,#lastname, #street_number").each(function(){
            if ($(this).val() != '') {
               var val = $(this).attr('name')+':  '+$(this).val();
               var mytextareaFld = $('#ta_holdAll');
              mytextareaFld.val(mytextareaFld.val() + val + '\n');
            }
        })
  }

http://jsfiddle.net/6JHRT/7/

于 2012-06-07T16:26:55.153 回答