助けが必要。
同じクラス名を持つ追加されたテキストボックスの値を取得するにはどうすればよいですか?コードスニペットを見つけてください。
JS:
var count = document.getElementsByName("emailId[]").length; //get the totol no. of contact no. elements
//function to add a new element for entering emailId
$("#add-email-id").click(function () {
$("#email-id-container").append('<div id="emailAddress_container_' + count + '" style="margin-top:8px"><input type="text" name="emailId[]" id="email_address_' + count + '" data-toggle="tooltip" title="Enter Email address here" placeholder="[email protected]" size="30" onBlur="ValidateEmail(' + count + ');" class="emailId"><span><input class="username" id="username_' + count + '" name="username[]" value="username" /></span><span style="margin-left:8px;"><a href="javascript:void(0);" class="delete-email-id" style="color:#F00;">(-)</a></span><span id="email_err_' + count + '"></span></div>');
count++; //increment the counter as new contact element is being added.
});
$(document).ready(function () {
$('.emailId').change(function () {
var usernameDiv = $(this).parent("div").find(".username").attr("id"); //get the div where the truncated username will be placed
alert($(this).attr("id"));
alert($("#" + usernameDiv).val());
var emailAddress = $(this).val();
var truncatedValue = emailAddress;
var name = "";
if (emailAddress.indexOf("@") != -1) {
///alert("@");
name = $.trim(emailAddress.split("@")[0]);
//alert(name);
if (name && name.search(/\s/) === -1) {
var len = name.length;
//alert(len);
if (len > 4) {
truncatedValue = name.substring(0, 3);
}
usernameDiv.val(truncatedValue);
} else {
usernameDiv.val("username");
}
}
});
});
email_addressフィールドの値を取得してから、追加された適切なユーザー名フィールドの切り捨てられた値を取得する必要があります。
$('.emailId').change(function() { //will not work for dynamic generated element
上記のコード行を次のように変更します
$(document).on('change', '.emailId', function() {
代わりに、イベントハンドラーにアタッチしてドキュメントを作成するか、要素の作成時に必ず.changeイベントを呼び出してください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加