好吧,我自己真的很困惑。
我们有这个应用程序可以连接值并将它们发送到 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>