我试图在每个输入字段的each()循环中使用before()方法。我需要为输入分配标签。 它不起作用。 但是,当我使用prepend方法时,它会在输入中插入标签。
var zuzu = jQuery("form#caspioform :input");
jQuery(zuzu).each(function(){
var label = jQuery("label[for='"+jQuery(this).attr('id')+"']"); //
jQuery(this).before(label.parent().html());
})
var zuzu = jQuery("form#caspioform :input");
jQuery(zuzu).each(function(){
var label = jQuery("label[for='"+jQuery(this).attr('id')+"']"); //
jQuery(this).prepend(label.parent().html());
})
FULL CODE删除表并将提取的输入字段添加到表单中:
var zuzu = jQuery("form#caspioform :input");
jQuery(zuzu).each(function() {
var label = jQuery("label[for='" + jQuery(this).attr('id') + "']"); //
jQuery(this).before(label.parent().html());
})
jQuery("#caspioform table").remove();
jQuery("#caspioform").append(zuzu);
<form style="margin: 0px;" action="http://b5.caspio.com/dp.asp" id="caspioform" method="post">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<table cellspacing="0" style="border-collapse: collapse;" id="cbTable_7aea6b2e1aa15b" name="cbTable">
<tbody>
<tr onmouseout="cbRowHover(this,'transparent');" onmouseover="cbRowHover(this,'none');" style="padding: 7px; background-color: transparent;">
<td style="text-align: left; vertical-align: top; padding: 7px;" colspan="2">
<h1>CONTACT OUR FIRM</h1>
<p>NOTE: Fields with a <span style="color:#ff0000">*</span> indicate a required field.</p>
</td>
</tr>
<tr>
<td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;">
<label for="InsertRecordName">Name:<span style="color: #FF0000;font-size: 12px;font-family: Arial ;margin-left: 2px;">*</span>
</label>
</td>
<td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;">
<input type="text" value="" id="InsertRecordName" name="InsertRecordName" maxlength="255" size="50" style="color: #666666; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: normal; border: 1px solid #d1d0d1; padding: 5px; margin: 4px 0;">
</td>
</tr>
<tr onmouseout="cbRowHover(this,'transparent');" onmouseover="cbRowHover(this,'none');" style="padding: 7px; background-color: transparent;">
<td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;">
<label for="InsertRecordEmail">Email:<span style="color: #FF0000;font-size: 12px;font-family: Arial ;margin-left: 2px;">*</span>
</label>
</td>
<td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;">
<input type="text" value="" id="InsertRecordEmail" name="InsertRecordEmail" maxlength="255" size="50" style="color: #666666; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: normal; border: 1px solid #d1d0d1; padding: 5px; margin: 4px 0;">
</td>
</tr>
<tr>
<td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;text-align: right;">
<label for="InsertRecordState">State:</label>
</td>
<td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;">
<select style="color: #666666;font-size: 12px;font-family: Arial ;font-style: normal;font-weight: normal;border: 1px solid #d1d0d1;padding: 10px;margin: 4px 0;" name="InsertRecordState" id="InsertRecordState">
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
<option value="Connecticut">Connecticut</option>
<option value="Delaware">Delaware</option>
<option value="District of Columbia">District of Columbia</option>
<option value="Florida">Florida</option>
<option value="Georgia">Georgia</option>
<option value="Guam">Guam</option>
<option value="Hawaii">Hawaii</option>
<option value="Idaho">Idaho</option>
<option selected="selected" value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
<option value="Louisiana">Louisiana</option>
<option value="Maine">Maine</option>
<option value="Maryland">Maryland</option>
<option value="Massachusetts">Massachusetts</option>
<option value="Michigan">Michigan</option>
<option value="Minnesota">Minnesota</option>
<option value="Mississippi">Mississippi</option>
<option value="Missouri">Missouri</option>
<option value="Montana">Montana</option>
<option value="Nebraska">Nebraska</option>
<option value="Nevada">Nevada</option>
<option value="New Hampshire">New Hampshire</option>
<option value="New Jersey">New Jersey</option>
<option value="New Mexico">New Mexico</option>
<option value="New York">New York</option>
<option value="North Carolina">North Carolina</option>
<option value="North Dakota">North Dakota</option>
<option value="Ohio">Ohio</option>
<option value="Oklahoma">Oklahoma</option>
<option value="Oregon">Oregon</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Puerto Rico">Puerto Rico</option>
<option value="Rhode Island">Rhode Island</option>
<option value="South Carolina">South Carolina</option>
<option value="South Dakota">South Dakota</option>
<option value="Tennessee">Tennessee</option>
<option value="Texas">Texas</option>
<option value="Utah">Utah</option>
<option value="Vermont">Vermont</option>
<option value="Virginia">Virginia</option>
<option value="Washington">Washington</option>
<option value="West Virginia">West Virginia</option>
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
</select>
</td>
</tr>
<tr onmouseout="cbRowHover(this,'transparent');" onmouseover="cbRowHover(this,'none');" style="padding: 7px; background-color: transparent;">
<td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;text-align: right;">
<label for="InsertRecordZIP">ZIP:</label>
</td>
<td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;">
<input type="text" value="" id="InsertRecordZIP" name="InsertRecordZIP" maxlength="10" size="10" style="color: #666666; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: normal; border: 1px solid #d1d0d1; padding: 5px; margin: 4px 0;">
</td>
</tr>
<tr>
<td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;">
<label for="InsertRecordPhone">Phone:<span style="color: #FF0000;font-size: 12px;font-family: Arial ;margin-left: 2px;">*</span>
</label>
</td>
<td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;">
<input type="text" value="" id="InsertRecordPhone" name="InsertRecordPhone" maxlength="14" size="20" style="color: #666666; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: normal; border: 1px solid #d1d0d1; padding: 5px; margin: 4px 0;">
</td>
</tr>
<tr onmouseout="cbRowHover(this,'transparent');" onmouseover="cbRowHover(this,'none');" style="padding: 7px; background-color: transparent;">
<td style="text-align: left; vertical-align: top; padding: 7px;" colspan="2">
<h2>How would you prefer to be contacted?</h2>
</td>
</tr>
<tr>
<td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;text-align: right;">
<label for="InsertRecordContact_Method"> </label>
</td>
<td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;">
<span "=" " style="color: #000000; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: normal; "><input type="radio " value="E-Mail " id="InsertRecordContact_Method0 " name="InsertRecordContact_Method "><label for="InsertRecordContact_Method0
">E-Mail</label><br><input type="radio " value="Phone " id="InsertRecordContact_Method1 " name="InsertRecordContact_Method "><label for="InsertRecordContact_Method1 ">Phone</label><br><input type="radio " value="No Preference " id="InsertRecordContact_Method2
" name="InsertRecordContact_Method "><label for="InsertRecordContact_Method2 ">No Preference</label><br></span></td></tr><tr onmouseout="cbRowHover(this, 'transparent'); " onmouseover="cbRowHover(this, 'none'); " style="padding: 7px; background-color:
transparent; "><td style="text-align: left; vertical-align: top; padding: 7px; " colspan="2 "><h2>Briefly describe your legal issue.</h2></td></tr><tr><td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px;
white-space: normal; color: #000000;font-size: 12px;font-family: Arial ;text-align: right; "><label for="InsertRecordMessage "> </label></td><td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap;
"><textarea id="InsertRecordMessage " name="InsertRecordMessage " onkeypress="return cbTextAreaHelper.controlKeyPress(event, this, 750); " onkeydown="return cbTextAreaHelper.controlKeyDown(event, this, 750); " oninput="cbTextAreaHelper.displayTip(this,
750, null, null, null, 0); " onchange="cbTextAreaHelper.displayTip(this, 750, null, null, null, 0); " onkeyup="cbTextAreaHelper.displayTip(this, 750, null, null, null, 0); " rows="5 " cols="50 " style="color: #666666; font-size: 12px;
font-family: Arial ; font-style: normal; font-weight: normal; border: 1px solid #d1d0d1; padding: 5px; margin:4px 0; "></textarea></td></tr><tr onmouseout="cbRowHover(this, 'transparent'); " onmouseover="cbRowHover(this, 'none');
" style="padding: 7px; background-color: transparent; "><td style="text-align: left; vertical-align: top; padding: 7px; " colspan="2 "><p><img align="left " style="padding-right: 10px; padding-bottom: 5px; " src="/images/formDisclaimer.gif
">The use of the Internet or this form for communication with the firm or any individual member of the firm does not establish an attorney-client relationship. Confidential or time-sensitive information should not be sent through this form.</p></td></tr><tr><td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ; "><label for="InsertRecordDisclaimer "> <span style="color: #FF0000;font-size: 12px;font-family:
Arial ;margin-left: 2px; ">*</span></label></td><td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap; "><span style="color: #000000; font-size: 12px; font-family: Arial ; font-style: normal; font-weight:
normal; "><input type="checkbox " value="YES " id="InsertRecordDisclaimer " name="InsertRecordDisclaimer "> <label for="InsertRecordDisclaimer ">I have read and understand the <a href="/disclaimer
">disclaimer</a>.</label></span></td></tr><tr onmouseout="cbRowHover(this, 'transparent'); " onmouseover="cbRowHover(this, 'none'); " style="padding: 7px; background-color: transparent; "><td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal;
color: #000000;font-size: 12px;font-family: Arial ;text-align: right; "><label for="cbParamCaptcha1@Image "> </label></td><td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap; "><img width="168
" height="59 " title="Click to reload new image " alt="Security code. Click to reload new image " src="http://b5.caspio.com/captchaSource/image.aspx?ctoken=7C50F65B2BEF13AA3AE42D86A808E017A8AD23C54BE8BAD83E346106F162A6E2877D4B7E511E922AC414FC2CC5D73E9C200D3589BE52FAA98E8439E84ECD71E1&t=34050
" onclick="try{var v_c=t his; v_c.src=v _c.src.replace(/[&][t][=][0-9]+$/gi, '') + '&t=' + String(new Date().getTime());}catch(e){} " id="captcha "><input type="hidden " value="7C50F65B2BEF13AA3AE42D86A808E017A8AD23C54BE8BAD83E346106F162A6E2877D4B7E511E922AC414FC2CC5D73E9C200D3589BE52FAA98E8439E84ECD71E1
" name="captchasession "></td></tr><tr><td style="text-align: right; vertical-align: middle; width: 90px; padding: 3px 5px; white-space: normal; color: #000000;font-size: 12px;font-family: Arial ; "><label for="cbParamCaptcha1
">Enter the code you see above.<span style="color: #FF0000;font-size: 12px;font-family: Arial ;margin-left: 2px; ">*</span></label></td><td style="text-align: left; vertical-align: top; padding: 3px 7px; white-space: nowrap; "><input type="text " value=" " id="cbParamCaptcha1
" name="cbParamCaptcha1 " maxlength="255 " size="25 " style="color: #666666; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: normal; border: 1px solid #d1d0d1; padding: 5px; margin: 4px 0; "><a href="# " onclick="f_ShowPlayer_7aea6b2e1aa15b();try{this.blur();}catch(v_e){}
return false; "><img border="0 " alt="Audio Captcha. Listen and type what you hear " title="Listen and type what you hear " src="http://b5.caspio.com/images/ac_handicap_icon_small.gif "></a><span id="AC_container_7aea6b2e1aa15b
"></span></td></tr><tr onmouseout="cbRowHover(this, 'transparent'); " onmouseover="cbRowHover(this, 'none'); " style="padding: 7px; "><td style="text-align: left; vertical-align: top; padding: 7px; " colspan="2 "><script language="JavaScript ">
function f_a(v_id)
{
return document.getElementById(v_id);
}
f_a('InsertRecordPhone').maxLength = 14;
f_a('InsertRecordPhone').onkeyup = function(v_e)
{
v_e = v_e || window.event;
if (v_e.keyCode >= 65 && v_e.keyCode <= 90){
this.value = this.value.substr(0, this.value.length - 1);
return false;
}else if (v_e.keyCode >= 37 && v_e.keyCode <= 40){
return true;
}
var v_value =(this.value.replace(/[^\d]/g, ''));
if (v_value.length==7) {
this.value = (v_value.substring(0,3) + "- " + v_value.substring(3,7));}
else if(v_value.length==10){
this.value = ("( " + v_value.substring(0,3) + ") " + v_value.substring(3,6) + "- " + v_value.substring(6,10));
};
}
</script></td></tr><tr><td style="padding: 10px 7px; text-align: center; vertical-align: middle; " colspan="2 "><input type="submit " onmouseout="cbButtonHover(this,
'color: #000000; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: bold; text-align: center; vertical-align: middle; width: auto; height: auto; margin: 0px 3px; padding: 8px 18px; background-color: #e1ddd1; border: 0;'); " onmouseover="cbButtonHover(this,
'color: #000000; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: bold; text-align: center; vertical-align: middle; background-color: #b3b0a6; width: auto; height: auto; margin: 0px 3px; padding: 8px 18px; border:0px;'); " style="color: #000000; font-size: 12px; font-family: Arial ; font-style: normal; font-weight: bold; text-align: center; vertical-align: middle; width: auto; height: auto; margin: 0px 3px; padding: 8px 18px; background-color: #e1ddd1;
border: 0; " value="Submit " id="Submit " name="Submit "></td></tr></tbody></table></td></tr></tbody></table><input type="hidden " value="cd283000f0a1d3c218d34909a075 " name="AppKey "><input type="hidden " value="_7aea6b2e1aa15b
" id="cbUniqueFormId_7aea6b2e1aa15b " name="cbUniqueFormId "><input type="hidden " value="3 " name="PrevPageID "><input type="hidden " value="http://www.cosleycriminaldefense.com/contact " name="pathname "><input type="hidden " value="1
" name="Mod0InsertRecord "><input type="hidden " value="3 " name="Mod0InsertRecordPageID "><input type="hidden " value="Caspio " name="cbAP "><input type="hidden " value="SYSDATE " name="InsertRecordSubmission_Date " id="InsertRecordSubmission_Date
"><input type="hidden " value="Contact Page " name="InsertRecordForm " id="InsertRecordForm "><input type="hidden " value="www.cosleycriminaldefense.com " name="InsertRecordWebsite " id="InsertRecordWebsite "></form>
答案 0 :(得分:0)
将标签添加到输入的父级。只需更改此行:
jQuery(this).prepend(label.parent().html());
为:
jQuery(this).parent().prepend(label.parent().html());
请注意parent()
之前的jQuery(this)
功能。您的代码现在应该可以使用了。