name
атрибут существует во входных элементах формы, связанных с веб-запросами (другими словами, когда идентификация данных выполняется на стороне сервера), в основном это элементы формы.
Вы можете определитьопределенный div
элемент с атрибутом id (который должен быть uniq в текущем документе) или класс, если логика касается нескольких элементов.
Вот предложение реализации:
$(function() {
// Cache references to both radio inputs at the top of the DOM-ready
// event handler making further logic clearer
let $nameInput = $('input#update-name')
, $descInput = $('input#update-description')
;
// Attach a single click event handler to both former radio inputs.
// Depending on which radio button is clicked, the associated content
// will be shown whereas the content associated to the other radio
// button will be hidden
$nameInput.add($descInput).on('click', function() {
let isName = this === $nameInput[0]
, $show = isName ? $('#nama-div') : $('#deskripsi-div')
, $hide = isName ? $('#deskripsi-div') : $('#nama-div')
;
if (this.checked) {
$show.fadeIn();
$hide.hide();
}
});
});
/** Set initial _display_ state of both content div as a CSS rule **/
div#nama-div, div#deskripsi-div {display: none}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mdl-textfield check-modif mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="check-modif">
<label class="mdl-textfield__label" for="check-modif">Nama atau Kode barang</label>
</div>
<label class="mdl-radio radio-modif-name mdl-js-radio mdl-js-ripple-effect" for="update-name">
<input type="radio" id="update-name" class="mdl-radio__button" name="options" value="1">
<span class="mdl-radio__label">Nama</span>
</label>
<label class="mdl-radio radio-modif-description mdl-js-radio mdl-js-ripple-effect" for="update-description">
<input type="radio" id="update-description" class="mdl-radio__button" name="options" value="2">
<span class="mdl-radio__label">Deskripsi</span>
</label>
<!-- changed name="nama" to id="nama-div" -->
<div class="mdl-textfield input-modif-name mdl-js-textfield" id="nama-div">
<input class="mdl-textfield__input" type="text" id="nama" name="nama">
<label class="mdl-textfield__label" for="nama">Nama</label></input>
</div>
<!-- changed name="deskripsi" to id="deskripsi-div" -->
<div class="mdl-textfield input-modif-description mdl-js-textfield" id="deskripsi-div">
<input class="mdl-textfield__input" type="text" id="deskripsi" name="deskripsi">
<label class="mdl-textfield__label" for="deskripsi">Deskripsi</label></input>
</div>