2014-11-03 2 views
0

Я пытаюсь добавить текст, если он не существует в форме, если она существует Donot добавить:JQuery текст Append, если оленья кожа существует

<form method="post" action="" class="horizontal-form"> 
<fieldset> 
<input type="file" align="left" name="patient_image[]" multiple="multiple" min="1" max="6"/> 
</fieldset> 
</form> 
jQuery(function(){ 


$('form').each(function() { 
    if ($(this).find('fieldset').html() === '<br/>Total image size is limited to 4MB. If you are uploading more than one image, the total combined size is still 4MB. Files should be formatted into JPG or BMP.') { 
     $("input[name*='patient_image']").after('<br/>Total image size is limited to 4MB. If you are uploading more than one image, the total combined size is still 4MB. Files should be formatted into JPG or BMP.'); 
    } 
}); 



}); 

Вот ссылка на скрипке я пытался до сих пор: http://jsfiddle.net/zbpv5zfk/37/

+0

Пожалуйста, ваши Javascript в самом заявлении вопрос, а просьба. – thatidiotguy

+0

Вы делаете это неправильно. Текст Fieldset не то, что вам нужно. Добавьте div с классом, проверьте, что класс существует в наборе полей. Если нет, добавьте его с текстом. – vaso123

+0

@lolka_bolka Я не могу добавить класс к нему, просто нужно добавить, как есть – govindak

ответ

1

jQuery(function() { 
 
    var sText = 'Total image size is limited to 4MB. If you are uploading more than one image, the total combined size is still 4MB. Files should be formatted into JPG or BMP.'; 
 
    $('form').find('fieldset:not(:contains("' + sText + '"))') 
 
    .find("input[name*='patient_image']").after('<br/>' + sText); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form method="post" action="" class="horizontal-form"> 
 
    <fieldset> 
 
    <input type="file" align="left" name="patient_image[]" multiple="multiple" min="1" max="6" /> 
 
    </fieldset> 
 
</form>

+0

http://jsfiddle.net/zbpv5zfk/40/ Я попытался добавить, если уже существующий контент добавляет его дважды, нужно добавить, если его нет, и если его уже не нужно. – govindak

+0

Ознакомьтесь с измененным кодом в моем ответе и этой демонстрацией: http://jsfiddle.net/fiddleyetu/zbpv5zfk/43/ Теперь все должно быть хорошо. В принципе вы не можете использовать ': contains()' для элемента - 'br' - но вы можете сделать': has() ', который нам не нужен, поскольку текст более идентифицирует, чем' br'. – PeterKA

+0

Спасибо, работал :) – govindak

0

Он никогда не произойдет, потому что если вы сваливать на консоль этого: $(this).find('fieldset').html() Валу этого будет:

`

    <input name="patient_image[]" multiple="multiple" min="1" max="6" align="left" type="file"> 
      [and a new line and a lot of space here] 

So, what Вы need это для do является check a part of your text, есть есть, и добавить, если не:

$(function() { 
    if ($(this).find('fieldset').html().indexOf('Total image size is limited to 4MB') === -1) { 
     $(this).find('fieldset').after('<br/>Total image size is limited to 4MB. If you are uploading more than one image, the total combined size is still 4MB. Files should be formatted into JPG or BMP.'); 
    } 
}); 

See working jsfiddle: http://jsfiddle.net/zbpv5zfk/38/

Of course вы можете use your целая цепочка тоже. Но я думаю, что это плохая практика, потому что если вы хотите что-то изменить в своем тексте, это трудно поддерживать.

Смежные вопросы